-
Notifications
You must be signed in to change notification settings - Fork 0
/
__.js
114 lines (74 loc) · 1.88 KB
/
__.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*** __ ***/
//------ monad: composition and chains ------
let __ =
(f, ...fs) => __.pipe(
typeof f !== 'function' ? () => f : f,
...fs
)
__.return =
x => () => x;
__.pipe =
(f=__.id, ...fs) => fs.length
? (...xs) => __.pipe(...fs)(f(...xs))
: (...xs) => f(...xs);
__.do =
(f=__.id, ...fs) => fs.length
? __.pipe(__.do(f), __.do(...fs))
: x => {f(x); return x}
//------ pull-back and push-forward ------
__.pull =
(...gs) => f => __.pipe(...gs, f);
__.push =
(...fs) => f => __.pipe(f, ...fs);
//------ argument application -------
__.$ =
(...xs) => f => f(...xs);
__.xargs =
f => xs => f(...xs);
__.bindl =
x => f => (...xs) => f(x, ...xs);
__.bindr =
y => f => (...xs) => f(...xs, y);
//------ logic ------
__.null =
() => {};
__.id =
x => x;
__.not =
b => !b;
__.if =
(f, g, h) =>
(...xs) => f(...xs) ? g(...xs) : h(...xs);
//------- arrays -------------
__.range =
n => [...Array(n).keys()];
__.linspace =
(t0, t1, n=20) => __.range(n)
.map(t => t0 + t * (t1 - t0) / (n - 1));
__.concat =
(as, bs) => [...as, ...bs];
__.map =
(...fs) =>
arr => Array.isArray(arr)
? arr.map(__.pipe(...fs))
: __.pipe(...fs)(arr);
__.map2 =
(...fs) =>
(as, bs) => as.map((ai, i) => __.pipe(...fs)(ai, bs[i], i));
__.apply =
fs => (...xs) => __.map(__.$(...xs))(fs);
__.filter =
f => arr => arr.filter(f);
__.reduce =
(f, acc) => arr => arr.reduce(f, acc);
//--------- z z z -----------------
__.sleep =
ms => new Promise(then => setTimeout(then, ms));
__.log =
x => {console.log(x); return x};
__.logs =
str =>
x => {__.log(str || 'logs:'); return __.log(x)};
//------ node exports ------
if (typeof module !== 'undefined')
module.exports = __;