Is there a `flip` function in the OCaml standard library?
Many functions like this for generalized FP plumbing aren't defined in the standard OCaml library. I have always missed them.
However, nowadays there are good OCaml libraries that supply most or all of these missing functions. The OCaml Batteries Included project defines flip
in the BatPervasives
module. The Jane Street Core project defines flip
in the Fn
module.
Now Fun.flip
in included in the standard library as of OCaml 4.08.
The pain is somewhat mitigated by labeled 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>
Thus if you're willing to write down the labels (which some argue makes code more readable) then you may be able to get the behavior you want. It depends on the situation of course.