functionalscript 0.0.214 → 0.0.215

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/function/index.js CHANGED
@@ -8,11 +8,11 @@
8
8
  const compose = f => g => x => f(g(x))
9
9
 
10
10
  /** @type {<T>(value: T) => T} */
11
- const id = value => value
11
+ const identity = value => value
12
12
 
13
13
  module.exports = {
14
14
  /** @readonly */
15
- id,
15
+ identity,
16
16
  /** @readonly */
17
17
  compose,
18
18
  }
package/json/test.js CHANGED
@@ -1,6 +1,6 @@
1
1
  const json = require('.')
2
2
  const { sort } = require('../object')
3
- const { id } = require('../function')
3
+ const { identity } = require('../function')
4
4
 
5
5
  if (json.addProperty("Hello")([])({}) !== "Hello") { throw 'error' }
6
6
 
@@ -10,7 +10,7 @@ if (json.addProperty("Hello")([])({}) !== "Hello") { throw 'error' }
10
10
  }
11
11
 
12
12
  {
13
- const x = json.stringify(id)(json.addProperty("Hello")(['a'])({}))
13
+ const x = json.stringify(identity)(json.addProperty("Hello")(['a'])({}))
14
14
  if (x !== '{"a":"Hello"}') { throw x }
15
15
  }
16
16
 
@@ -20,7 +20,7 @@ if (json.addProperty("Hello")([])({}) !== "Hello") { throw 'error' }
20
20
  }
21
21
 
22
22
  {
23
- const x = json.stringify(id)(json.addProperty("Hello")(['a'])({ c: [], b: 12 }))
23
+ const x = json.stringify(identity)(json.addProperty("Hello")(['a'])({ c: [], b: 12 }))
24
24
  if (x !== '{"c":[],"b":12,"a":"Hello"}') { throw x }
25
25
  }
26
26
 
@@ -34,6 +34,6 @@ if (json.addProperty("Hello")([])({}) !== "Hello") { throw 'error' }
34
34
  {
35
35
  const _0 = { a: { y: [24] }, c: [], b: 12 }
36
36
  const _1 = json.addProperty("Hello")(['a', 'x'])(_0)
37
- const _2 = json.stringify(id)(_1)
37
+ const _2 = json.stringify(identity)(_1)
38
38
  if (_2 !== '{"a":{"y":[24],"x":"Hello"},"c":[],"b":12}') { throw _2 }
39
39
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.214",
3
+ "version": "0.0.215",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/sequence/index.js CHANGED
@@ -18,7 +18,7 @@ const { logicalNot, strictEqual } = require('../function/operator')
18
18
  *
19
19
  * Please note that the sequence also contains `Concat<T>. We need this as
20
20
  * a workaround because modern JavaScript implementations don't support
21
- * ES6 TCO (Tail Call Optimization). Without this wotkaround we may have
21
+ * ES6 TCO (Tail Call Optimization). Without this workaround, we may have
22
22
  * a stack overflow if a list contains a lot of concateneted lists.
23
23
  *
24
24
  * @template T
@@ -37,6 +37,9 @@ const { logicalNot, strictEqual } = require('../function/operator')
37
37
 
38
38
  const empty = () => undefined
39
39
 
40
+ /** @type {<T>(first: T) => (tail: Sequence<T>) => Sequence<T>} */
41
+ const sequence = first => tail => () => [first, tail]
42
+
40
43
  /** @type {<F, T>(a: readonly[F, Sequence<T>]) => (b: Sequence<T>) => readonly[F, Sequence<T>]} */
41
44
  const norm = ([a0, a1]) => b => [a0, [a1, b]]
42
45
 
@@ -78,7 +81,7 @@ const first = input => {
78
81
  */
79
82
 
80
83
  /**
81
- * Note: the operation is not lazy. It traverse the given array and creates a linked list.
84
+ * Note: the operation is not lazy. It traverses the given array and creates a linked list.
82
85
  *
83
86
  * @type {<T>(...array: readonly T[]) => Sequence<T>}
84
87
  */
@@ -86,14 +89,12 @@ const list = (...array) => {
86
89
  /** @typedef {typeof array extends readonly(infer T)[] ? T : never} T */
87
90
  let i = array.length
88
91
  /** @type {Sequence<T>} */
89
- let result = empty
92
+ let iResult = empty
90
93
  while (i !== 0) {
91
94
  i = i - 1
92
- /** @type {FirstAndTail<T>} */
93
- const listResult = [array[i], result]
94
- result = () => listResult
95
+ iResult = sequence(array[i])(iResult)
95
96
  }
96
- return result
97
+ return iResult
97
98
  }
98
99
 
99
100
  /** @type {<T>(...array: readonly Sequence<T>[]) => Sequence<T>} */
@@ -273,10 +274,9 @@ const reverse = s => {
273
274
  while (true) {
274
275
  const result = next(iSource)
275
276
  if (result === undefined) { return iResult }
276
- /** @type {typeof s} */
277
- const old = iResult
278
- iResult = () => [result[0], old]
279
- iSource = result[1]
277
+ const [first, tail] = result
278
+ iResult = sequence(first)(iResult)
279
+ iSource = tail
280
280
  }
281
281
  }
282
282
 
@@ -288,6 +288,8 @@ module.exports = {
288
288
  /** @readonly */
289
289
  empty,
290
290
  /** @readonly */
291
+ sequence,
292
+ /** @readonly */
291
293
  at,
292
294
  /** @readonly */
293
295
  concat,
package/sequence/test.js CHANGED
@@ -1,14 +1,15 @@
1
1
  const seq = require('.')
2
+ const { empty, next, list, flatMap, concat, exclusiveScan, find, every, some, first, drop, map, generate } = require('.')
2
3
  const { sum } = require('./operator')
3
4
  const array = require('./array')
4
5
  const json = require('../json')
5
- const { id } = require('../function')
6
+ const { identity } = require('../function')
6
7
 
7
8
  /** @type {<T>(input: seq.Sequence<T>) => void} */
8
9
  const print = input => {
9
10
  let i = input
10
11
  while (true) {
11
- const result = seq.next(i)
12
+ const result = next(i)
12
13
  if (result === undefined) { return }
13
14
  console.log(result[0])
14
15
  i = result[1]
@@ -16,47 +17,47 @@ const print = input => {
16
17
  }
17
18
 
18
19
  {
19
- const big = seq.list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 42, 60)
20
- const list0 = seq.list(0, 1, 2, 3)
21
- const list1 = seq.flatMap(x => seq.list(x, x * 2, x * 3))(list0)
22
- const list2 = seq.concat(list0, list0)
23
- const list3 = seq.exclusiveScan(sum)(list0)
24
- const r = seq.find(x => x === 42)(big)
25
- if (seq.every(x => x > 0)(big) !== true) { throw 'x'}
26
- if (seq.every(x => x < 20)(big) !== false) { throw 'x' }
27
- if (seq.some(x => x > 100)(big) !== false) { throw 'x' }
28
- if (seq.some(x => x > 50)(big) !== true) { throw 'x' }
29
- if (seq.first(seq.drop(16)(big)) !== 42) { throw 'drop'}
20
+ const big = list(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 42, 60)
21
+ const list0 = list(0, 1, 2, 3)
22
+ const list1 = flatMap(x => list(x, x * 2, x * 3))(list0)
23
+ const list2 = concat(list0, list0)
24
+ const list3 = exclusiveScan(sum)(list0)
25
+ const r = find(x => x === 42)(big)
26
+ if (every(x => x > 0)(big) !== true) { throw 'x' }
27
+ if (every(x => x < 20)(big) !== false) { throw 'x' }
28
+ if (some(x => x > 100)(big) !== false) { throw 'x' }
29
+ if (some(x => x > 50)(big) !== true) { throw 'x' }
30
+ if (first(drop(16)(big)) !== 42) { throw 'drop' }
30
31
  {
31
- const a = seq.map(seq.generate)(seq.generate(100_000))
32
+ const a = map(generate)(generate(100_000))
32
33
  const ar = array.fromSequence(a)
33
- // This operation use a lot of stack because `...`
34
+ // This operation uses a lot of stack because `...`
34
35
  // puts array items on a stack.
35
36
  // Use `array.sequence` instead
36
- const x = seq.concat(...ar)
37
- const r = seq.next(x)
37
+ const x = concat(...ar)
38
+ const r = next(x)
38
39
  // print(x)
39
40
  }
40
41
  {
41
- const a = array.fromSequence(seq.generate(1_000_000))
42
- let x = seq.concat(array.sequence(a), big)
43
- const r = seq.next(x)
42
+ const a = array.fromSequence(generate(1_000_000))
43
+ let x = concat(array.sequence(a), big)
44
+ const r = next(x)
44
45
  // print(x)
45
46
  }
46
47
  {
47
48
  let x = big
48
49
  for (let i = 0; i < 1_000_000; ++i) {
49
- x = seq.concat(seq.empty, x)
50
+ x = concat(empty, x)
50
51
  }
51
- const r = seq.next(x)
52
+ const r = next(x)
52
53
  // print(x)
53
- }
54
+ }
54
55
  {
55
56
  let x = big
56
57
  for (let i = 0; i < 1_000_000; ++i) {
57
- x = seq.concat(x, seq.list(i))
58
+ x = concat(x, list(i))
58
59
  }
59
- const r = seq.next(x)
60
+ const r = next(x)
60
61
  // print(x)
61
62
  }
62
63
  }
@@ -69,6 +70,6 @@ const print = input => {
69
70
  {
70
71
  const r = seq.reverse(seq.list(1, 2, 3, 4))
71
72
  const s = array.fromSequence(r)
72
- const j = json.stringify(id)(s)
73
+ const j = json.stringify(identity)(s)
73
74
  if (j !== '[4,3,2,1]') { throw j }
74
75
  }