-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (78 loc) · 2.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//. # Callgebra
//.
//. The little algebra of callbacks.
//.
//. ## Introduction
//.
//. This package exports functions for composing callbacks. You can read
//. about the idea in my [Medium post about composable callbacks][1].
//.
//. ## Usage
//.
//. ### Node
//.
//. ```console
//. $ npm install --save callgebra
//. ```
//.
//. On Node 12 and up, this module can be loaded directly with `import` or
//. `require`. On Node versions below 12, `require` or the [esm][]-loader can
//. be used.
//.
//. ```js
//. import {of, callback} from 'callgebra';
//. callback (console.log) (of (42));
//. ```
//.
//. ### Deno and Modern Browsers
//.
//. You can load the EcmaScript module from various content delivery networks:
//.
//. - [Skypack](https://cdn.skypack.dev/[email protected])
//. - [JSPM](https://jspm.dev/[email protected])
//. - [jsDelivr](https://cdn.jsdelivr.net/npm/[email protected]/+esm)
//.
//. ### Old Browsers and Code Pens
//.
//. There's a [UMD][] file included in the NPM package, also available via
//. jsDelivr: https://cdn.jsdelivr.net/npm/[email protected]/dist/umd.js
//.
//. This file adds `flutureProject` to the global scope, or use CommonJS/AMD
//. when available.
//.
//. ```js
//. const {of, callback} = require ('callgebra');
//. callback (console.log) (of (42));
//. ```
//.
//. ## API
//.
//. ```hs
//. type Callback a b = (b -> a) -> a
//. ```
// thrush :: a -> (a -> b) -> b
const thrush = x => f => f (x);
//# of :: a -> Callback b a
//.
//. Creates a callback for a given return value.
export const of = thrush;
//# chain :: (a -> Callback c b) -> Callback c a -> Callback c b
//.
//. Sequence two callback-accepting functions.
export const chain = f => m => c => m (x => f (x) (c));
//# map :: (a -> b) -> Callback c a -> Callback c b
//.
//. Modify the return value of a callback.
export const map = f => chain (x => thrush (f (x)));
//# ap :: Callback c (a -> b) -> Callback c a -> Callback c b
//.
//. Apply the function returned by one callback to the value returned by
//. another.
export const ap = mf => mx => chain (f => map (f) (mx)) (mf);
//# callback :: (a -> b) -> Callback b a -> b
//.
//. Given a function and a Callback, runs the Callback using the function.
export const callback = thrush;
//. [1]: https://medium.com/@avaq/composable-callbacks-81c84f0324
//. [esm]: https://github.com/standard-things/esm
//. [UMD]: https://github.com/umdjs/umd