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 +2 -2
- package/json/test.js +4 -4
- package/package.json +1 -1
- package/sequence/index.js +13 -11
- package/sequence/test.js +27 -26
package/function/index.js
CHANGED
package/json/test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
const json = require('.')
|
|
2
2
|
const { sort } = require('../object')
|
|
3
|
-
const {
|
|
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(
|
|
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(
|
|
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(
|
|
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
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
|
|
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
|
|
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
|
|
92
|
+
let iResult = empty
|
|
90
93
|
while (i !== 0) {
|
|
91
94
|
i = i - 1
|
|
92
|
-
|
|
93
|
-
const listResult = [array[i], result]
|
|
94
|
-
result = () => listResult
|
|
95
|
+
iResult = sequence(array[i])(iResult)
|
|
95
96
|
}
|
|
96
|
-
return
|
|
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
|
-
|
|
277
|
-
|
|
278
|
-
|
|
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 {
|
|
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 =
|
|
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 =
|
|
20
|
-
const list0 =
|
|
21
|
-
const list1 =
|
|
22
|
-
const list2 =
|
|
23
|
-
const list3 =
|
|
24
|
-
const r =
|
|
25
|
-
if (
|
|
26
|
-
if (
|
|
27
|
-
if (
|
|
28
|
-
if (
|
|
29
|
-
if (
|
|
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 =
|
|
32
|
+
const a = map(generate)(generate(100_000))
|
|
32
33
|
const ar = array.fromSequence(a)
|
|
33
|
-
// This operation
|
|
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 =
|
|
37
|
-
const r =
|
|
37
|
+
const x = concat(...ar)
|
|
38
|
+
const r = next(x)
|
|
38
39
|
// print(x)
|
|
39
40
|
}
|
|
40
41
|
{
|
|
41
|
-
const a = array.fromSequence(
|
|
42
|
-
let x =
|
|
43
|
-
const r =
|
|
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 =
|
|
50
|
+
x = concat(empty, x)
|
|
50
51
|
}
|
|
51
|
-
const r =
|
|
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 =
|
|
58
|
+
x = concat(x, list(i))
|
|
58
59
|
}
|
|
59
|
-
const r =
|
|
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(
|
|
73
|
+
const j = json.stringify(identity)(s)
|
|
73
74
|
if (j !== '[4,3,2,1]') { throw j }
|
|
74
75
|
}
|