There's a neat operator in ReasonML that's called a "fast pipe" it looks like this:
-> or like this
|.. And it allows for a nice visual flow when calling functions that take a type as their first argument (it we were using an object-oriented language, these functions might be defined as methods on an object).
The new release of ReasonML
3.3.x deprecated the use of
|. as the fast pipe operator, preferring instead the skinny arrow syntax:
->. We'll probably see both of them in documentation and examples for a while.
The fast pipe takes everything on the left side of the operator, and applies the result of that expression as the first argument of the function on the right side of the operator.
This sort of simulates what it feels like to call a method on an object in an object-oriented language.
For example, if we had a class
Person in Python, with a method
greet, that took a message as a string, we could call it like this:
In Reason, the practice is to create a
person type, but keep any functions that work on the
person type as stand-alone functions that take the
person type as the first argument. Something like this:
let greet = (person, string) => string;
See above that
greet takes a person, and a string, and returns another string that, presumably, greets that person.
To call this in Reason without fast piping, we'd do this:
let greeting = greet(personA, "hello");
But if we want to feel more like
greet is a method instead of a standalone function, we can use the fast-pipe operator:
let greeting = personA->greet("hello");
/* or */
let greeting = personA|.greet("hello");
See how the thing on the left (personA) gets applied as the first argument of the function on the right?
This might seem marginally useful. But we really start to see the beauty once we start chaining function calls:
maybeSomething->Belt.Option.map(a => a)->Belt.Option.getExn
"Fast piping" is a way to conveniently call functions that take an object (or type) as their first parameter. The preferred syntax is
->, but is also exists as