Search code examples
gofunctional-programmingoperators

How can I get built-in operator as a function?


The goal

How can I get a built-in operator as a function? I want to take advantage of functional programming. I have some files with vectorizing functions (eg. Remap[inT any](sl []inT, f func(inT) inT)), which take a scalar function to apply on every element of one or two slices.

I know I could:
func eq(a, b int)bool{
   return a==b
}

and hope that after inlining there'll be no overhead.


But I prefer a short, performant & consistent way, if exists.

  • In Python 3 I would:
    int.__eq__
  • In Rust I would:
    i32::eq
  • In C++ I would:
   #include <functional>
   /*code here*/
   std::equal_to<int>()
How would you achieve this in Go?

Solution

  • How can I get built-in operator as a function? Any functional package in Go?

    You cannot.