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.
@@ -1,6 +1,13 @@
1
- const { pipe } = require('../func')
1
+ const { todo } = require('../../dev')
2
+ const { pipe } = require('../../func')
3
+ const mapReduce = require('../../map-reduce')
2
4
 
3
- /** @type {<T, R>(f: (value: T) => Promise<R>) => (c: AsyncIterable<T>) => AsyncIterable<R>} */
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(async x => f(x)))(flatten)
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) => Promise<A>} ReduceFn
45
+ * @typedef {(accumulator: A) => (value: T) => PromiseOrValue<A>} Merge
39
46
  */
40
47
 
41
- /** @type {<A, T>(f: ReduceFn<A, T>) => (init: A) => (c: AsyncIterable<T>) => Promise<A>} */
42
- const reduce = reduceFn => init => async c => {
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 reduceFn(result)(i)
52
+ result = await merge(result)(i)
46
53
  }
47
54
  return result
48
55
  }
49
56
 
50
- /** @type {<A, T>(f: ReduceFn<A, T>) => (init: A) => (c: AsyncIterable<T>) => AsyncIterable<A>} */
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: ReduceFn<A, T>) => (init: A) => (c: AsyncIterable<T>) => AsyncIterable<A>} */
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 {(c: AsyncIterable<number>) => Promise<number>} */
82
- const sum = reduce(a => async v => a + v)(0)
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 = op => pipe(reduce(op.reduce)(op.init))(op.result)
14
+ const apply = ({ merge, init, result }) => pipe(reduce(merge)(init))(result)
15
15
 
16
16
  module.exports = {
17
17
  /** @readonly */
@@ -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 reduce: (state: S) => (value: I) => S
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 => ({ reduce, result, init}) => ({
16
- reduce: pipe(reduce)(pipe(mapFn)),
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
- reduce: s => i => s === undefined ? i : `${s}${separator}${i}`,
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
- reduce: a => i => a + i,
36
+ merge: a => i => a + i,
31
37
  result: id,
32
38
  init: 0,
33
39
  }
@@ -41,7 +41,7 @@ const mr = require('../map-reduce')
41
41
 
42
42
  /** @type {mr.Operation<string, undefined|Path, undefined|Path>} */
43
43
  const pathNormReduce = {
44
- reduce: path => item =>
44
+ merge: path => item =>
45
45
  path === undefined ?
46
46
  undefined :
47
47
  ['', '.'].includes(item) ?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.161",
3
+ "version": "0.0.162",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const i = require('./')
2
2
 
3
- require('./async/test')
3
+ require('./async/iterable/test')
4
4
  require('./module-manager/test')
5
5
 
6
6
  /** @type {() => never} */