Is there a function composition operator in reason?


#1

Is there operators similar to >> or << from elm-lang or . of haskell


#2

@@

http://caml.inria.fr/pub/docs/manual-ocaml/libref/Stdlib.html#VAL(@@)


#3

Not exactly. Point-free style is not considered idiomatic in Reason/OCaml as it is in Haskell. There are a couple of operators which can compose functions and apply an argument at the same time: https://reasonml.github.io/api/Pervasives.html#6_Compositionoperators


#4

well the problem with those is that you need to provide the parameter upfront but what I would like to do out of box is

List.map [1,2,3] (((+)5)>>String.fromInt)

In the elm code above I didnt have to write a closure to be able to pass the variable around I simply composed a new function from simpler functions.

('a=>'b)=>('b=>'c)=>('a=>'c)

I created the following

let (>>) = (a: 'a => 'b, b: 'b => 'c): ('a => 'c) => {
  let helper = vl => vl->a->b;
  helper;
};

Is there any way to do this whithout the helper function in there?


#5

IMHO, this is quite difficult to follow. I would this much easier to read:

List.map(n => String.fromInt(n + 5), [1, 2, 3])

And it’s just a few more characters. Or if you want to pipe everything, maybe even:

List.map(n => n |> (+)(5) |> String.fromInt, [1, 2, 3])

But really it doesn’t gain you much.