functionalscript 0.0.167 → 0.0.168

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.
@@ -83,7 +83,7 @@ const exclusiveScan = merge => init => c => ({
83
83
  }
84
84
  })
85
85
 
86
- /** @type {<T, R>(es: seq.ExlusiveScan<T, R>) => (c: AnyIterable<T>) => AsyncIterable<R>} */
86
+ /** @type {<T, R>(es: seq.Scan<T, R>) => (c: AnyIterable<T>) => AsyncIterable<R>} */
87
87
  const applyExclusiveScan = es => c => ({
88
88
  async *[Symbol.asyncIterator]() {
89
89
  let ies = es
package/iterable/index.js CHANGED
@@ -38,8 +38,8 @@ const exclusiveScan = merge => init => c => ({
38
38
  /** @type {<A, T>(merge: Merge<A, T>) => (init: A) => (c: Iterable<T>) => Iterable<A>} */
39
39
  const inclusiveScan = merge => init => c => concat([init])(exclusiveScan(merge)(init)(c))
40
40
 
41
- /** @type {<T, R>(es: seq.ExlusiveScan<T, R>) => (c: Iterable<T>) => Iterable<R>} */
42
- const applyExclusiveScan = es => c => ({
41
+ /** @type {<T, R>(es: seq.Scan<T, R>) => (c: Iterable<T>) => Iterable<R>} */
42
+ const applyScan = es => c => ({
43
43
  *[Symbol.iterator]() {
44
44
  let ies = es
45
45
  for (const i of c) {
@@ -50,7 +50,10 @@ const applyExclusiveScan = es => c => ({
50
50
  }
51
51
  })
52
52
 
53
- const entries = applyExclusiveScan(seq.entries)
53
+ /** @type {<T, R>(is: seq.InclusiveScan<T, R>) => (c: Iterable<T>) => Iterable<R>} */
54
+ const applyInclusiveScan = ({scan, first}) => c => concat([first])(applyScan(scan)(c))
55
+
56
+ const entries = applyScan(seq.entries)
54
57
 
55
58
  /** @type {<I, S, R>(op: seq.Operation<I, S, R>) => (_: Iterable<I>) => R} */
56
59
  const apply = ({ merge, init, result }) => pipe(reduce(merge)(init))(result)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.167",
3
+ "version": "0.0.168",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/sequence/index.js CHANGED
@@ -1,29 +1,96 @@
1
- const { pipe, id } = require('../function')
1
+ const { todo } = require('../dev')
2
+ const { id } = require('../function')
2
3
 
3
4
  /**
4
- * @template S
5
- * @template I
6
- * @typedef {(state: S) => (value: I) => S} Merge
5
+ * @template T0
6
+ * @template T1
7
+ * @typedef {import('../array').Tuple2<T0, T1>} Tuple2
8
+ */
9
+
10
+ /**
11
+ * @template T
12
+ * @template R
13
+ * @typedef {Tuple2<R, Scan<T, R>>} ScanResult
14
+ */
15
+
16
+ /**
17
+ * @template T
18
+ * @template R
19
+ * @typedef {(value: T) => ScanResult<T, R>} Scan
7
20
  */
8
21
 
22
+ /**
23
+ * @template T
24
+ * @template R
25
+ * @typedef {{
26
+ * readonly scan: Scan<T, R>
27
+ * readonly first: R
28
+ * }} InclusiveScan
29
+ */
30
+
31
+ /**
32
+ * @template R
33
+ * @template T
34
+ * @typedef {(prior: R) => (value: T) => R} BinaryOperator
35
+ */
36
+
37
+ /** @type {<R, T>(operator: BinaryOperator<R, T>) => (prior: R) => Scan<T, R>} */
38
+ const operatorScan = operator => {
39
+ /** @typedef {typeof operator extends BinaryOperator<infer R, infer T> ? [R, T] : never} RT */
40
+ /** @typedef {RT[0]} R */
41
+ /** @typedef {RT[1]} T */
42
+ /** @type {(prior: R) => Scan<T, R>} */
43
+ const f = prior => value => {
44
+ const result = operator(prior)(value)
45
+ return [result, f(result)]
46
+ }
47
+ return f
48
+ }
49
+
50
+ /** @type {<R, T>(operator: BinaryOperator<R, T>) => (first: R) => InclusiveScan<T, R>} */
51
+ const inclusiveOperatorScan = operator => first => ({
52
+ scan: operatorScan(operator)(first),
53
+ first,
54
+ })
55
+
56
+ /**
57
+ * @template T
58
+ * @typedef {Tuple2<number, T>} Entry
59
+ */
60
+
61
+ /** @type {(index: number) => <T>(value: T) => ScanResult<T, Entry<T>>} */
62
+ const createEntries = index => value => [[index, value], createEntries(index + 1)]
63
+
64
+ const entries = createEntries(0)
65
+
66
+ /** @type {(separator: string) => BinaryOperator<string, string>} */
67
+ const joinOperation = separator => prior => value => `${prior}${separator}${value}`
68
+
69
+ /** @type {(separator: string) => InclusiveScan<string, string>} */
70
+ const join2 = separator => ({
71
+ scan: value => [value, operatorScan(joinOperation(separator))(value)],
72
+ first: ''
73
+ })
74
+
75
+ /** @type {(sum: number) => (value: number) => number} */
76
+ const addition = a => b => a + b
77
+
78
+ /** @type {InclusiveScan<number, number>} */
79
+ const sum2 = inclusiveOperatorScan(addition)(0)
80
+
81
+ ////
82
+
9
83
  /**
10
84
  * @template I
11
85
  * @template S
12
86
  * @template O
13
87
  * @typedef {{
14
- * readonly merge: Merge<S, I>
88
+ * readonly merge: BinaryOperator<S, I>
15
89
  * readonly result: (state: S) => O
16
90
  * readonly init: S
17
91
  * }} Operation
18
92
  */
19
93
 
20
- /** @type {<I, T>(mapFn: (value: I) => T) => <S, O>(op: Operation<T, S, O>) => Operation<I, S, O>} */
21
- const map = mapFn => ({ merge, result, init}) => ({
22
- merge: pipe(merge)(pipe(mapFn)),
23
- result,
24
- init,
25
- })
26
-
27
94
  /** @type {(separator: string) => Operation<string, string|undefined, string>} */
28
95
  const join = separator => ({
29
96
  merge: s => i => s === undefined ? i : `${s}${separator}${i}`,
@@ -51,37 +118,10 @@ const size = {
51
118
  result: id,
52
119
  }
53
120
 
54
- /**
55
- * @template T
56
- * @template R
57
- * @typedef {(value: T) => readonly [R, ExlusiveScan<T, R>]} ExlusiveScan
58
- */
59
-
60
- /**
61
- * @template T
62
- * @template R
63
- * @template I
64
- * @typedef {{
65
- * readonly exlusive: ExlusiveScan<T, R>
66
- * readonly default: I
67
- * }} InclusiveScan
68
- */
69
-
70
- /**
71
- * @template T
72
- * @typedef {readonly[number, T]} Entry
73
- */
74
-
75
- /** @type {(index: number) => <T>(value: T) => readonly[Entry<T>, ExlusiveScan<T, Entry<T>>]} */
76
- const entriesFrom = index => value => [[index, value], entriesFrom(index + 1)]
77
-
78
- const entries = entriesFrom(0)
79
-
80
121
  module.exports = {
81
- /** @readonly */
82
- map,
83
122
  /** @readonly */
84
123
  join,
124
+ join2,
85
125
  /** @readonly */
86
126
  sum,
87
127
  /** @readonly */