functionalscript 0.0.176 → 0.0.178

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.176",
3
+ "version": "0.0.178",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -45,12 +45,26 @@ const fromArray = a => {
45
45
  return at(0)
46
46
  }
47
47
 
48
- /** @type {<T>(list0: List<T>) => ListMap<T, T>} */
49
- const concat = a => b => () => {
50
- /** @typedef {typeof a extends List<infer T> ? T : never} T */
51
- /** @type {(firstAntTail: FirstAndTail<T>) => Result<T>} */
52
- const defined = ([first, tail]) => [first, concat(tail)(b)]
53
- return option.match(defined)(b)(a())
48
+ /**
49
+ * Note: the function is not completly lazy.
50
+ * it calls `a()` as soon as `a` and `b` are provided.
51
+ * Othrewise we may have a stack overflow if a list
52
+ * contains a lot of concateneted empty lists.
53
+ * And we can't relay on ES6 TCO (Tail Call Optimization)
54
+ * because it's not supported by Chrome and Firefox.
55
+ * @type {<T>(list0: List<T>) => ListMap<T, T>}
56
+ */
57
+ const concat = a => b => {
58
+ const result = a()
59
+ if (result !== undefined) {
60
+ const [first, tail] = result
61
+ return () => [first, concat(tail)(b)]
62
+ }
63
+ return b
64
+ // /** @typedef {typeof a extends List<infer T> ? T : never} T */
65
+ // /** @type {(firstAntTail: FirstAndTail<T>) => Result<T>} */
66
+ // const defined = ([first, tail]) => [first, concat(tail)(b)]
67
+ // return option.match(defined)(b)(a())
54
68
  }
55
69
 
56
70
  /** @type {<T, R>(f: (value: T) => List<R>) => ListMap<T, R>} */
@@ -12,14 +12,25 @@ const print = a => {
12
12
 
13
13
  {
14
14
  const big = list.fromArray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 42, 60])
15
- // print(big)
16
- /*
17
15
  const list0 = list.fromArray([0, 1, 2, 3])
18
16
  const list1 = list.flatMap(x => list.fromArray([x, x * 2, x * 3]))(list0)
19
17
  const list2 = list.concat(list0)(list0)
20
18
  const list3 = list.inclusiveScan(sum)(list0)
21
- print(list3)
22
- */
23
19
  const r = list.find(x => x === 42)(big)
24
- console.log(r)
20
+ {
21
+ let x = big
22
+ for (let i = 0; i < 10000; ++i) {
23
+ x = list.concat(list.empty)(x)
24
+ }
25
+ const r = x()
26
+ print(x)
27
+ }
28
+ {
29
+ let x = big
30
+ for (let i = 0; i < 10000; ++i) {
31
+ x = list.concat(x)(list.empty)
32
+ }
33
+ const r = x()
34
+ print(x)
35
+ }
25
36
  }