functionalscript 0.0.161 → 0.0.162
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/async/{index.js → iterable/index.js} +22 -11
- package/async/{test.js → iterable/test.js} +0 -0
- package/iterable/index.js +1 -1
- package/map-reduce/index.js +11 -5
- package/module-manager/index.js +1 -1
- package/package.json +1 -1
- package/test.js +1 -1
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const { todo } = require('../../dev')
|
|
2
|
+
const { pipe } = require('../../func')
|
|
3
|
+
const mapReduce = require('../../map-reduce')
|
|
2
4
|
|
|
3
|
-
/**
|
|
5
|
+
/**
|
|
6
|
+
* @template T
|
|
7
|
+
* @typedef {Promise<T>|T} PromiseOrValue
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/** @type {<T, R>(f: (value: T) => PromiseOrValue<R>) => (c: AsyncIterable<T>) => AsyncIterable<R>} */
|
|
4
11
|
const map = f => c => ({
|
|
5
12
|
async *[Symbol.asyncIterator]() {
|
|
6
13
|
for await (const i of c) {
|
|
@@ -30,24 +37,24 @@ const filter = f => c => ({
|
|
|
30
37
|
})
|
|
31
38
|
|
|
32
39
|
/** @type {<T, R>(f: (value: T) => AsyncIterable<R>) => (c: AsyncIterable<T>) => AsyncIterable<R>} */
|
|
33
|
-
const flatMap = f => pipe(map(
|
|
40
|
+
const flatMap = f => pipe(map(f))(flatten)
|
|
34
41
|
|
|
35
42
|
/**
|
|
36
43
|
* @template A
|
|
37
44
|
* @template T
|
|
38
|
-
* @typedef {(accumulator: A) => (value: T) =>
|
|
45
|
+
* @typedef {(accumulator: A) => (value: T) => PromiseOrValue<A>} Merge
|
|
39
46
|
*/
|
|
40
47
|
|
|
41
|
-
/** @type {<A, T>(f:
|
|
42
|
-
const reduce =
|
|
48
|
+
/** @type {<A, T>(f: Merge<A, T>) => (init: A) => (c: AsyncIterable<T>) => Promise<A>} */
|
|
49
|
+
const reduce = merge => init => async c => {
|
|
43
50
|
let result = init
|
|
44
51
|
for await (const i of c) {
|
|
45
|
-
result = await
|
|
52
|
+
result = await merge(result)(i)
|
|
46
53
|
}
|
|
47
54
|
return result
|
|
48
55
|
}
|
|
49
56
|
|
|
50
|
-
/** @type {<A, T>(f:
|
|
57
|
+
/** @type {<A, T>(f: Merge<A, T>) => (init: A) => (c: AsyncIterable<T>) => AsyncIterable<A>} */
|
|
51
58
|
const exclusiveScan = reduceFn => init => c => ({
|
|
52
59
|
async *[Symbol.asyncIterator]() {
|
|
53
60
|
let result = init
|
|
@@ -58,7 +65,7 @@ const exclusiveScan = reduceFn => init => c => ({
|
|
|
58
65
|
}
|
|
59
66
|
})
|
|
60
67
|
|
|
61
|
-
/** @type {<A, T>(f:
|
|
68
|
+
/** @type {<A, T>(f: Merge<A, T>) => (init: A) => (c: AsyncIterable<T>) => AsyncIterable<A>} */
|
|
62
69
|
const inclusiveScan = reduceFn => init => {
|
|
63
70
|
const e = exclusiveScan(reduceFn)(init)
|
|
64
71
|
return c => ({
|
|
@@ -78,10 +85,14 @@ const cast = iterable => ({
|
|
|
78
85
|
}
|
|
79
86
|
})
|
|
80
87
|
|
|
81
|
-
/** @type {(
|
|
82
|
-
const
|
|
88
|
+
/** @type {<I, S, R>(op: mapReduce.Operation<I, S, R>) => (_: AsyncIterable<I>) => Promise<R>} */
|
|
89
|
+
const apply = ({ merge, init, result }) => async c => result(await reduce(merge)(init)(c))
|
|
90
|
+
|
|
91
|
+
const sum = apply(mapReduce.sum)
|
|
83
92
|
|
|
84
93
|
module.exports = {
|
|
94
|
+
/** @readonly */
|
|
95
|
+
apply,
|
|
85
96
|
/** @readonly */
|
|
86
97
|
cast,
|
|
87
98
|
/** @readonly */
|
|
File without changes
|
package/iterable/index.js
CHANGED
|
@@ -11,7 +11,7 @@ const reduce = merge => init => c => {
|
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
/** @type {<I, S, R>(op: mr.Operation<I, S, R>) => (_: Iterable<I>) => R} */
|
|
14
|
-
const apply =
|
|
14
|
+
const apply = ({ merge, init, result }) => pipe(reduce(merge)(init))(result)
|
|
15
15
|
|
|
16
16
|
module.exports = {
|
|
17
17
|
/** @readonly */
|
package/map-reduce/index.js
CHANGED
|
@@ -1,33 +1,39 @@
|
|
|
1
1
|
const { pipe, id } = require('../func')
|
|
2
2
|
|
|
3
|
+
/**
|
|
4
|
+
* @template S
|
|
5
|
+
* @template I
|
|
6
|
+
* @typedef {(state: S) => (value: I) => S} Merge
|
|
7
|
+
*/
|
|
8
|
+
|
|
3
9
|
/**
|
|
4
10
|
* @template I
|
|
5
11
|
* @template S
|
|
6
12
|
* @template O
|
|
7
13
|
* @typedef {{
|
|
8
|
-
* readonly
|
|
14
|
+
* readonly merge: Merge<S, I>
|
|
9
15
|
* readonly result: (state: S) => O
|
|
10
16
|
* readonly init: S
|
|
11
17
|
* }} Operation
|
|
12
18
|
*/
|
|
13
19
|
|
|
14
20
|
/** @type {<I, T>(mapFn: (value: I) => T) => <S, O>(op: Operation<T, S, O>) => Operation<I, S, O>} */
|
|
15
|
-
const map = mapFn => ({
|
|
16
|
-
|
|
21
|
+
const map = mapFn => ({ merge, result, init}) => ({
|
|
22
|
+
merge: pipe(merge)(pipe(mapFn)),
|
|
17
23
|
result,
|
|
18
24
|
init,
|
|
19
25
|
})
|
|
20
26
|
|
|
21
27
|
/** @type {(separator: string) => Operation<string, string|undefined, string>} */
|
|
22
28
|
const join = separator => ({
|
|
23
|
-
|
|
29
|
+
merge: s => i => s === undefined ? i : `${s}${separator}${i}`,
|
|
24
30
|
init: undefined,
|
|
25
31
|
result: s => s === undefined ? '' : s
|
|
26
32
|
})
|
|
27
33
|
|
|
28
34
|
/** @type {Operation<number, number, number>} */
|
|
29
35
|
const sum = {
|
|
30
|
-
|
|
36
|
+
merge: a => i => a + i,
|
|
31
37
|
result: id,
|
|
32
38
|
init: 0,
|
|
33
39
|
}
|
package/module-manager/index.js
CHANGED
package/package.json
CHANGED