Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an infix function composition operator in OCaml?

Just a quick question. I'm wondering if there is a infix function composition operator in OCaml defined in the standard library (or in Jane Street's Core or in Batteries) like the (.) function in Haskell which saves us a lot parentheses since we can write (f . g . h) x instead of the less appealing f (g (h x))).

Thanks folks.

like image 600
tfboy Avatar asked May 19 '13 16:05

tfboy


3 Answers

The answer here is the same as for flip :-). Function composition isn't defined in the OCaml standard library. In this case, it isn't something I miss once in a while, I miss it all the time.

The OCaml Batteries Included project defines function composition (in the order you give) using the operator -| in the BatStd module. As lukstafi points out (see below), this operator will apparently change to % in a future release of Batteries. (I've verified this in their source tree.)

As far as I can see, the Jane Street Core project doesn't define a function composition operator. It defines a function compose in the Fn module.

like image 59
Jeffrey Scofield Avatar answered Oct 18 '22 20:10

Jeffrey Scofield


I just want to add that the operator is fairly easy to include, in F# it's simply defined as:

let (<<) f g x = f(g(x));;

which has the type signature: val ( << ) : f:('a -> 'b) -> g:('c -> 'a) -> x:'c -> 'b doing exactly what you need...

(f << g << h) x = f(g(h(x))

so you don't need the batteries project if you don't have to

I'd like to add that the reason it looks like << is, as you might guess, because the >> operator does the opposite:

let (>>) f g x = g(f(x));;

(f >> g >> h) x = h(g(f(x))
like image 18
Electric Coffee Avatar answered Oct 18 '22 20:10

Electric Coffee


There is Fn.compose function in Core, but it is not an infix operator. Also, it is implemented as a regular function and has runtime overhead.

In practice, it is pretty convenient to use pipe operator. It has no runtime overhead as implemented directly in compiler (starting from 4.00). See Optimized Pipe Operators for more details.

Pipe operator is available as '|>' in Core. So, you can rewrite your expression as following: h x |> g |> f

like image 16
Stas Avatar answered Oct 18 '22 20:10

Stas