-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
177 lines (150 loc) · 3.32 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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Functions
//
export function autoPartial (fn) {
return function (...args) {
if (args.length >= fn.length) {
return fn(...args)
} else {
return fn.bind(this, ...args)
}
}
}
const _ = autoPartial
export function flow (...fns) {
return (input) => reduce((lastResult, fn) => fn(lastResult), input, fns)
}
export const pipe = _(function pipe (value, ...fns) {
return flow(...fns)(value)
})
//
// Values
//
export function isUndefined (a) {
return typeof a === 'undefined'
}
export function isPromise (a) {
return a instanceof Promise
}
export function isFunction (a) {
return typeof a === 'function'
}
export function isIterable (a) {
return isFunction(a[Symbol.iterator])
}
//
// Iterables
//
export const reduce = _(function reduce (reducer, start, input) {
let result = start
for (const e of input) {
result = reducer(result, e)
}
return result
})
export const map = _(function * map (mapper, input) {
for (const e of input) {
yield mapper(e)
}
})
export const filter = _(function * filter (filterer, input) {
for (const e of input) {
if (filterer(e)) {
yield e
}
}
})
export const take = _(function * take (n, input) {
let outputCount = 0
for (const e of input) {
yield e
if (++outputCount >= n) {
return
}
}
})
export function toArray (input) {
return [...input]
}
export function head (input) {
return [...take(1, input)][0]
}
export function * tail (input) {
let isFirst = true
for (const e of input) {
if (isFirst) {
isFirst = false
} else {
yield e
}
}
}
export const splitWhen = _(function * splitWhen (predicate, input) {
let nextBatch = []
for (const e of input) {
nextBatch.push(e)
if (predicate(e, nextBatch)) {
yield nextBatch
nextBatch = []
}
}
if (nextBatch.length > 0) {
yield nextBatch
}
})
export const split = _(function split (batchSize, input) {
const splitter = (_, batch) => batch.length >= batchSize
return splitWhen(splitter, input)
})
// Returns an iterable that yields values from each input iterable
// TODO
export const merge = _(function * merge (...iterables) {
for (const iterable of iterables) {
for (const e of iterable) {
yield e
}
}
})
// Returns an iterable that yields each grandchild of `iterable`
// [[a, b, c], [d, [e]]] -> [a, b, c, d, [e]]
export function * flatten (iterable) {
for (const child of iterable) {
if (isIterable(child)) {
for (const grandchild of child) {
yield grandchild
}
} else {
yield child
}
}
}
// Returns an iterable that yields each nested descendant of `iterable`
export function * flattenDeep (iterable) {
for (const child of iterable) {
if (isIterable(child)) {
for (const grandchild of flatten(child)) {
yield grandchild
}
} else {
yield child
}
}
}
//
// Promises
//
// Returns a lazy async thunk that resolves to `value` after at least `delayInMs`
export function delay (delayInMs) {
return (value) => new Promise((resolve) => {
setTimeout(() => resolve(value), delayInMs)
})
}
export function flowAsync (...fns) {
return (input) => reduce((lastPromise, fn) => lastPromise.then(fn),
Promise.resolve(input),
fns
)
}
export function pipeAsync (value, ...fns) {
return flowAsync(...fns)(value)
}