Is there a flip function in the OCaml standard library?

In Haskell, we have a flip function: flip fxy = fyx , which essentially takes a function and returns the same function, except that both arguments are swapped. I wonder if there is an analogue in OCaml, because I could not find it and I do not want to rewrite it every time.

Greetings

+8
ocaml
source share
2 answers

Many functions like this for general plumbing are not defined in the OCaml standard library. I always missed them.

However, there are currently good OCaml libraries that provide most or all of these missing features. OCaml batteries are included flip in the BatPervasives module. The Jane Street Core project defines flip in the Fn module.

+12
source share

The pain is somewhat mitigated by tagged arguments:

 # let f ~x ~y = x - y;; val f : x:int -> y:int -> int = <fun> # f ~y:5;; - : x:int -> int = <fun> # f ~x:6;; - : y:int -> int = <fun> 

That way, if you want to write down shortcuts (which some claim to make the code more readable), you can get the behavior you need. Of course, it depends on the situation.

+2
source share

All Articles