https://github.com/austindd/reason-arrow
Stack-safe function composition in ReasonML/ReScript
https://github.com/austindd/reason-arrow
Last synced: 4 months ago
JSON representation
Stack-safe function composition in ReasonML/ReScript
- Host: GitHub
- URL: https://github.com/austindd/reason-arrow
- Owner: austindd
- License: mit
- Created: 2020-11-18T22:49:41.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2020-11-23T19:36:18.000Z (over 5 years ago)
- Last Synced: 2025-03-01T04:23:49.306Z (over 1 year ago)
- Language: Reason
- Size: 48.8 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# reason-arrow
Stack-safe function composition in ReasonML/ReScript
## Usage
Use `Arrow` to compose functions lazily, while also maintaining stack safety, no matter how many functions you compose.
```reason
let add = (a, b) => a + b;
let sub = (a, b) => a - b;
let mul = (a, b) => a * b;
let div = (a, b) => a / b;
// myArrow: Arrow.t(int, int);
let myArrow = Arrow.({
pure(add(_, 4))
->pipeR(mul(_, 3))
->pipeR(sub(_, 4))
->pipeR(div(_, 1))
});
// ((((0 + 4) * 3) - 4) / 1) = 8
let result = Arrow.runF(myArrow, 0);
```
We get nice abstractions over lazy function composition. Here is an example of using `pipeR` with `map`:
```reason
let addFloat = (a, b) => a +. b;
let subFloat = (a, b) => a -. b;
let mulFloat = (a, b) => a *. b;
let divFloat = (a, b) => a /. b;
// useIntegersInstead: (float => float) => (int => int)
let useIntegersInstead = (fn, a) => fn(float_of_int(a))->int_of_float;
// myArrow: Arrow.t(int, int)
let myArrow = Arrow.({
pure(addFloat(_, 4.))
->pipeR(mulFloat(_, 3.))
->pipeR(subFloat(_, 4.))
->pipeR(divFloat(_, 1.))
->map(_, useIntegersInstead(_))
});
let result = Arrow.runF(myArrow, 0); // 8
```