functionalscript 0.0.231 → 0.0.232
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/commonjs/index.js +11 -0
- package/commonjs/path/index.js +64 -0
- package/commonjs/path/test.js +47 -0
- package/commonjs/run/index.js +17 -0
- package/io/commonjs/index.js +34 -0
- package/io/commonjs/test.js +72 -0
- package/json/index.js +22 -26
- package/json/test.js +9 -9
- package/package.json +23 -23
- package/test.js +4 -6
- package/{sequence → types}/array/index.js +2 -2
- package/{btree → types/btree}/README.md +0 -0
- package/{btree → types/btree}/index.js +6 -6
- package/{btree → types/btree}/test.js +1 -1
- package/{cmp → types/function/compare}/index.js +6 -6
- package/{function → types/function}/index.js +0 -0
- package/{function → types/function}/operator/index.js +15 -0
- package/{map → types/map}/index.js +3 -3
- package/{map → types/map}/test.js +0 -0
- package/{object → types/object}/index.js +1 -2
- package/{object → types/object}/test.js +0 -0
- package/{option → types/option}/index.js +0 -0
- package/types/result/index.js +28 -0
- package/{sequence → types/sequence}/README.md +0 -0
- package/{sequence → types/sequence}/index.js +83 -36
- package/{sequence → types/sequence}/test.js +138 -11
- package/version.js +2 -2
- package/module-manager/README.md +0 -35
- package/module-manager/index.js +0 -110
- package/module-manager/node/index.js +0 -18
- package/module-manager/node/test.js +0 -75
- package/module-manager/test.js +0 -120
- package/result/index.js +0 -17
- package/sequence/asyncIterable/index.js +0 -111
- package/sequence/asyncIterable/test.js +0 -24
- package/sequence/iterable/index.js +0 -109
- package/sequence/iterable/test.js +0 -51
- package/sequence/operator/index.js +0 -92
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
const mm = require('..')
|
|
2
|
-
const i = require('.')
|
|
3
|
-
|
|
4
|
-
/** @type {{ readonly [_ in string]: string}} */
|
|
5
|
-
const files = {
|
|
6
|
-
'index.js': './index.js',
|
|
7
|
-
'a/index.js': './a/index.js',
|
|
8
|
-
'a/b/x.js': './a/b/x.js',
|
|
9
|
-
'c/t.js': './c/t.js',
|
|
10
|
-
'node_modules/@functionalscript/functionalscript/package.json': '',
|
|
11
|
-
'node_modules/@functionalscript/functionalscript/index.js': '@functionalscript/functionalscript ./index.js',
|
|
12
|
-
'node_modules/my/package.json': '',
|
|
13
|
-
'node_modules/my/src/x.js': 'my ./src/x.js',
|
|
14
|
-
'node_modules/my/b/index.js': 'my ./b/index.js',
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** @type {(_: string) => string|undefined} */
|
|
18
|
-
const readFile = path => files[path]
|
|
19
|
-
|
|
20
|
-
const root = i(readFile)
|
|
21
|
-
|
|
22
|
-
{
|
|
23
|
-
const index = mm.getModule(root)('.')
|
|
24
|
-
if (index === undefined) { throw 'no module' }
|
|
25
|
-
if (index.fileName !== 'index.js') { throw 'fileName' }
|
|
26
|
-
if (index.source !== './index.js') { throw 'source' }
|
|
27
|
-
if (index.location.local.join('/') !== '') { throw 'location' }
|
|
28
|
-
if (index.location.pack.id.join('/') !== '') { throw 'pack.id' }
|
|
29
|
-
if (mm.moduleId(index) !== 'index.js') { throw 'moduleId' }
|
|
30
|
-
const a = mm.getModule(index.location)('./a')
|
|
31
|
-
if (a === undefined) { throw 'no module' }
|
|
32
|
-
if (a.fileName !== 'index.js') { throw 'fileName' }
|
|
33
|
-
if (a.source !== './a/index.js') { throw 'source' }
|
|
34
|
-
if (a.location.local.join('/') !== 'a') { throw 'location' }
|
|
35
|
-
if (a.location.pack.id.join('/') !== '') { throw 'pack.id' }
|
|
36
|
-
if (mm.moduleId(a) !== 'a/index.js') { throw 'moduleId' }
|
|
37
|
-
const abx = mm.getModule(a.location)('./b/x')
|
|
38
|
-
if (abx === undefined) { throw 'no module' }
|
|
39
|
-
if (abx.fileName !== 'x.js') { throw 'fileName' }
|
|
40
|
-
if (abx.source !== './a/b/x.js') { throw 'source' }
|
|
41
|
-
if (abx.location.local.join('/') !== 'a/b') { throw 'location' }
|
|
42
|
-
if (abx.location.pack.id.join('/') !== '') { throw 'pack.id' }
|
|
43
|
-
if (mm.moduleId(abx) !== 'a/b/x.js') { throw 'moduleId' }
|
|
44
|
-
const ct = mm.getModule(abx.location)('../../c/t.js')
|
|
45
|
-
if (ct === undefined) { throw 'no module' }
|
|
46
|
-
if (ct.fileName !== 't.js') { throw 'fileName' }
|
|
47
|
-
if (ct.source !== './c/t.js') { throw 'source' }
|
|
48
|
-
if (ct.location.local.join('/') !== 'c') { throw 'location' }
|
|
49
|
-
if (ct.location.pack.id.join('/') !== '') { throw 'pack.id' }
|
|
50
|
-
if (mm.getModule(ct.location)('./d') !== undefined) { throw 'no module' }
|
|
51
|
-
if (mm.getModule(ct.location)('@functionalscript') !== undefined) { throw 'no module' }
|
|
52
|
-
if (mm.moduleId(ct) !== 'c/t.js') { throw 'moduleId' }
|
|
53
|
-
const fs = mm.getModule(ct.location)('@functionalscript/functionalscript')
|
|
54
|
-
if (fs === undefined) { throw 'no module' }
|
|
55
|
-
if (fs.source !== '@functionalscript/functionalscript ./index.js') { throw 'source' }
|
|
56
|
-
if (fs.fileName !== 'index.js') { throw 'fileName' }
|
|
57
|
-
if (fs.location.local.join('/') !== '') { throw 'location' }
|
|
58
|
-
if (fs.location.pack.id.join('/') !== 'node_modules/@functionalscript/functionalscript') { throw 'pack.id' }
|
|
59
|
-
if (mm.getModule(fs.location)('my/src/x/') !== undefined) { throw 'no module '}
|
|
60
|
-
if (mm.moduleId(fs) !== 'node_modules/@functionalscript/functionalscript/index.js') { throw 'moduleId' }
|
|
61
|
-
const mySrcX = mm.getModule(fs.location)('my/src/x')
|
|
62
|
-
if (mySrcX === undefined) { throw 'no module' }
|
|
63
|
-
if (mySrcX.fileName !== 'x.js') { throw 'fileName' }
|
|
64
|
-
if (mySrcX.source !== 'my ./src/x.js') { throw 'source' }
|
|
65
|
-
if (mySrcX.location.local.join('/') !== 'src') { throw 'location' }
|
|
66
|
-
if (mySrcX.location.pack.id.join('/') !== 'node_modules/my') { throw 'pack.id' }
|
|
67
|
-
if (mm.moduleId(mySrcX) !== 'node_modules/my/src/x.js') { throw 'moduleId' }
|
|
68
|
-
const myB = mm.getModule(mySrcX.location)('../b/')
|
|
69
|
-
if (myB === undefined) { throw 'no module' }
|
|
70
|
-
if (myB.fileName !== 'index.js') { throw 'fileName' }
|
|
71
|
-
if (myB.source !== 'my ./b/index.js') { throw 'source' }
|
|
72
|
-
if (myB.location.local.join('/') !== 'b') { throw 'location' }
|
|
73
|
-
if (myB.location.pack.id.join('/') !== 'node_modules/my') { throw 'pack.id' }
|
|
74
|
-
if (mm.moduleId(myB) !== 'node_modules/my/b/index.js') { throw 'moduleId' }
|
|
75
|
-
}
|
package/module-manager/test.js
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
const i = require('.')
|
|
2
|
-
|
|
3
|
-
require('./node/test')
|
|
4
|
-
|
|
5
|
-
/** @type {<T>(_: T | undefined) => T} */
|
|
6
|
-
const cast = x => {
|
|
7
|
-
if (x === undefined) { throw 'x' }
|
|
8
|
-
return x
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
if (i.isRelative('a/b/c'.split('/'))) { throw 'error '}
|
|
12
|
-
if (!i.isRelative('./a/b/c'.split('/'))) { throw 'error ' }
|
|
13
|
-
if (cast(i.pathNorm('a/../b'.split('/'))).join('/') !== 'b') { throw 'error ' }
|
|
14
|
-
if (cast(i.pathNorm('a/../b/../c'.split('/'))).join('/') !== 'c') { throw 'error ' }
|
|
15
|
-
if (cast(i.pathNorm('./a/../b/c/..//d/'.split('/'))).join('/') !== 'b/d') { throw 'error ' }
|
|
16
|
-
|
|
17
|
-
{
|
|
18
|
-
/** @type {i.Package} */
|
|
19
|
-
const a = {
|
|
20
|
-
dependencies: () => undefined,
|
|
21
|
-
file: path => {
|
|
22
|
-
/** @type {{ [_ in string]?: string}} */
|
|
23
|
-
const f = {
|
|
24
|
-
'index.js': 'a ./index.js',
|
|
25
|
-
}
|
|
26
|
-
return f[path]
|
|
27
|
-
},
|
|
28
|
-
id: ['a']
|
|
29
|
-
}
|
|
30
|
-
/** @type {i.Package} */
|
|
31
|
-
const c = {
|
|
32
|
-
dependencies: () => undefined,
|
|
33
|
-
file: path => {
|
|
34
|
-
/** @type {{ readonly [_ in string]?: string}} */
|
|
35
|
-
const f = {
|
|
36
|
-
'index.js': 'b/c ./index.js',
|
|
37
|
-
'x/index.js': 'b/c ./x/index.js',
|
|
38
|
-
}
|
|
39
|
-
if (['.js', '', 'undefined.js'].includes(path)) { throw '.js' }
|
|
40
|
-
return f[path]
|
|
41
|
-
},
|
|
42
|
-
id: ['c']
|
|
43
|
-
}
|
|
44
|
-
/** @type {{ readonly [_ in string]: i.Package|i.Dependencies}} */
|
|
45
|
-
const packages = {
|
|
46
|
-
a,
|
|
47
|
-
b: s => {
|
|
48
|
-
/** @type {{ readonly [_ in string]: i.Package|i.Dependencies}} */
|
|
49
|
-
const p = { c }
|
|
50
|
-
return p[s]
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
/** @type {i.Package} */
|
|
54
|
-
const pack = {
|
|
55
|
-
dependencies: s => packages[s],
|
|
56
|
-
file: path => {
|
|
57
|
-
/** @type {{ readonly [_ in string]?: string}} */
|
|
58
|
-
const f = {
|
|
59
|
-
'index.js': './index.js',
|
|
60
|
-
'index/index.js': './index/index.js',
|
|
61
|
-
'a/index.js': './a/index.js',
|
|
62
|
-
'a/index.js.js': './a/index.js.js',
|
|
63
|
-
}
|
|
64
|
-
if (['.js', '', 'undefined.js'].includes(path)) { throw '.js' }
|
|
65
|
-
return f[path]
|
|
66
|
-
},
|
|
67
|
-
id: ['']
|
|
68
|
-
}
|
|
69
|
-
/** @type {(_: i.Module|undefined) => (_: i.Module) => void} */
|
|
70
|
-
const expect = a => b => {
|
|
71
|
-
if (a === undefined) { throw 'undefined' }
|
|
72
|
-
if (a.location.local.join('/') !== b.location.local.join('/')) { throw 'local'}
|
|
73
|
-
if (a.location.pack !== b.location.pack) { throw 'pack' }
|
|
74
|
-
if (a.source !== b.source) { throw 'source' }
|
|
75
|
-
}
|
|
76
|
-
{
|
|
77
|
-
const g = i.getModule({pack, local: []})
|
|
78
|
-
if (g('') !== undefined) { throw 'error' }
|
|
79
|
-
if (g('..') !== undefined) { throw 'error' }
|
|
80
|
-
expect(g('.'))({ fileName: 'index.js', location: { pack, local: []}, source: './index.js'})
|
|
81
|
-
expect(g('./index'))({ fileName: 'index.js', location: { pack, local: []}, source: './index.js'})
|
|
82
|
-
expect(g('./index.js'))({ fileName: 'index.js', location: { pack, local: []}, source: './index.js'})
|
|
83
|
-
expect(g('./index/'))({ fileName: 'index.js', location: { pack, local: ['index']}, source: './index/index.js'})
|
|
84
|
-
expect(g('./a'))({ fileName: 'index.js', location: { pack, local: ['a']}, source: './a/index.js'})
|
|
85
|
-
expect(g('./a/index'))({ fileName: 'index.js', location: { pack, local: ['a']}, source: './a/index.js'})
|
|
86
|
-
expect(g('./a/index.js'))({ fileName: 'index.js.js', location: { pack, local: ['a']}, source: './a/index.js'})
|
|
87
|
-
if (g('./x') !== undefined) { throw 'error' }
|
|
88
|
-
expect(g('a'))({ fileName: 'index.js', location: { pack: a, local: []}, source: 'a ./index.js'})
|
|
89
|
-
expect(g('a/index'))({ fileName: 'index.js', location: { pack: a, local: []}, source: 'a ./index.js'})
|
|
90
|
-
if (g('b') !== undefined) { throw 'error' }
|
|
91
|
-
expect(g('b/c'))({ fileName: 'index.js', location: { pack: c, local: []}, source: 'b/c ./index.js'})
|
|
92
|
-
expect(g('b/c/index'))({ fileName: 'index.js', location: { pack: c, local: []}, source: 'b/c ./index.js'})
|
|
93
|
-
expect(g('b/c/index.js'))({ fileName: 'index.js', location: { pack: c, local: []}, source: 'b/c ./index.js'})
|
|
94
|
-
expect(g('b/c/x'))({ fileName: 'index.js', location: { pack: c, local: ['x']}, source: 'b/c ./x/index.js'})
|
|
95
|
-
expect(g('b/c/r/..'))({ fileName: 'index.js', location: { pack: c, local: []}, source: 'b/c ./index.js'})
|
|
96
|
-
}
|
|
97
|
-
{
|
|
98
|
-
const g = i.getModule({pack, local: ['index']})
|
|
99
|
-
if (g('') !== undefined) { throw 'error' }
|
|
100
|
-
expect(g('..'))({ fileName: 'index.js', location: { pack, local: []}, source: './index.js'})
|
|
101
|
-
expect(g('.'))({ fileName: 'index.js', location: { pack, local: ['index']}, source: './index/index.js'})
|
|
102
|
-
expect(g('./index'))({ fileName: 'index.js', location: { pack, local: ['index']}, source: './index/index.js'})
|
|
103
|
-
expect(g('./index.js'))({ fileName: 'index.js', location: { pack, local: ['index']}, source: './index/index.js'})
|
|
104
|
-
if (g('./index/') !== undefined) { throw 'error' }
|
|
105
|
-
if (g('./a') !== undefined) { throw 'error' }
|
|
106
|
-
expect(g('../a'))({ fileName: 'index.js', location: { pack, local: ['a']}, source: './a/index.js'})
|
|
107
|
-
expect(g('../a/index'))({ fileName: 'index.js', location: { pack, local: ['a']}, source: './a/index.js'})
|
|
108
|
-
expect(g('../a/index.js'))({ fileName: 'index.js', location: { pack, local: ['a']}, source: './a/index.js'})
|
|
109
|
-
expect(g('../a/index.js.js'))({ fileName: 'index.js.js', location: { pack, local: ['a']}, source: './a/index.js.js'})
|
|
110
|
-
if (g('./x') !== undefined) { throw 'error' }
|
|
111
|
-
expect(g('a'))({ fileName: 'index.js', location: { pack: a, local: []}, source: 'a ./index.js'})
|
|
112
|
-
expect(g('a/index'))({ fileName: 'index.js', location: { pack: a, local: []}, source: 'a ./index.js'})
|
|
113
|
-
if (g('b') !== undefined) { throw 'error' }
|
|
114
|
-
expect(g('b/c'))({ fileName: 'index.js', location: { pack: c, local: []}, source: 'b/c ./index.js'})
|
|
115
|
-
expect(g('b/c/index'))({ fileName: 'index.js', location: { pack: c, local: []}, source: 'b/c ./index.js'})
|
|
116
|
-
expect(g('b/c/index.js'))({ fileName: 'index.js', location: { pack: c, local: []}, source: 'b/c ./index.js'})
|
|
117
|
-
expect(g('b/c/x'))({ fileName: 'index.js', location: { pack: c, local: ['x']}, source: 'b/c ./x/index.js'})
|
|
118
|
-
expect(g('b/c/r/..'))({ fileName: 'index.js', location: { pack: c, local: []}, source: 'b/c ./index.js'})
|
|
119
|
-
}
|
|
120
|
-
}
|
package/result/index.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
const { compose } = require('../../function')
|
|
2
|
-
const seq = require('../operator')
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @template T
|
|
6
|
-
* @typedef {Promise<T>|T} PromiseOrValue
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @template T
|
|
11
|
-
* @typedef {Iterable<T> | AsyncIterable<T>} AsyncOrSyncIterable
|
|
12
|
-
*/
|
|
13
|
-
|
|
14
|
-
/** @type {<T>(c: AsyncOrSyncIterable<AsyncOrSyncIterable<T>>) => AsyncIterable<T>} */
|
|
15
|
-
const flat = c => ({
|
|
16
|
-
async *[Symbol.asyncIterator]() {
|
|
17
|
-
for await (const i of c) {
|
|
18
|
-
yield* i
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
|
|
23
|
-
/** @type {<T, R>(f: (value: T) => AsyncOrSyncIterable<R>) => (c: AsyncOrSyncIterable<T>) => AsyncIterable<R>} */
|
|
24
|
-
const flatMap = f => c => ({
|
|
25
|
-
async *[Symbol.asyncIterator]() {
|
|
26
|
-
for await (const i of c) {
|
|
27
|
-
yield* await f(i)
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
/** @type {<T, R>(f: (value: T) => R) => (c: AsyncOrSyncIterable<T>) => AsyncIterable<R>} */
|
|
33
|
-
const map = seq.map(flatMap)
|
|
34
|
-
|
|
35
|
-
/** @type {<T>(f: (value: T) => boolean) => (c: AsyncOrSyncIterable<T>) => AsyncOrSyncIterable<T>} */
|
|
36
|
-
const filter = seq.filter(flatMap)
|
|
37
|
-
|
|
38
|
-
/** @type {<T>(a: AsyncOrSyncIterable<T>) => (b: AsyncOrSyncIterable<T>) => AsyncIterable<T>} */
|
|
39
|
-
const concat = a => b => ({
|
|
40
|
-
async *[Symbol.asyncIterator]() {
|
|
41
|
-
yield* a
|
|
42
|
-
yield* b
|
|
43
|
-
}
|
|
44
|
-
})
|
|
45
|
-
|
|
46
|
-
/** @type {<T, R>(s: seq.Scan<T, R>) => (c: AsyncOrSyncIterable<T>) => AsyncIterable<R>} */
|
|
47
|
-
const scan = s => c => ({
|
|
48
|
-
async *[Symbol.asyncIterator]() {
|
|
49
|
-
let next = s
|
|
50
|
-
for await (const i of c) {
|
|
51
|
-
const result = next(i)
|
|
52
|
-
next = result[1]
|
|
53
|
-
yield result[0]
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
})
|
|
57
|
-
|
|
58
|
-
/** @type {<T, R>(s: seq.ExclusiveScan<T, R>) => (c: AsyncOrSyncIterable<T>) => AsyncIterable<R>} */
|
|
59
|
-
const exclusiveScan = ([first, s]) => c => concat([first])(scan(s)(c))
|
|
60
|
-
|
|
61
|
-
/** @type {<T, R>(is: seq.ExclusiveScan<T, R>) => (c: AsyncOrSyncIterable<T>) => Promise<R>} */
|
|
62
|
-
const reduce = ([first, s]) => async c => {
|
|
63
|
-
let next = first
|
|
64
|
-
for await (const i of scan(s)(c)) {
|
|
65
|
-
next = i
|
|
66
|
-
}
|
|
67
|
-
return next
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const sum = reduce(seq.sum)
|
|
71
|
-
|
|
72
|
-
const join = compose(seq.join)(reduce)
|
|
73
|
-
|
|
74
|
-
const length = reduce(seq.length)
|
|
75
|
-
|
|
76
|
-
/** @type {<T>(f: (value: T) => boolean) => (c: AsyncOrSyncIterable<T>) => AsyncIterable<T>} */
|
|
77
|
-
const takeWhile = f => c => ({
|
|
78
|
-
async *[Symbol.asyncIterator]() {
|
|
79
|
-
for await (const i of c) {
|
|
80
|
-
if (!f(i)) { return }
|
|
81
|
-
yield i
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
})
|
|
85
|
-
|
|
86
|
-
module.exports = {
|
|
87
|
-
/** @readonly */
|
|
88
|
-
concat,
|
|
89
|
-
/** @readonly */
|
|
90
|
-
flat,
|
|
91
|
-
/** @readonly */
|
|
92
|
-
map,
|
|
93
|
-
/** @readonly */
|
|
94
|
-
filter,
|
|
95
|
-
/** @readonly */
|
|
96
|
-
flatMap,
|
|
97
|
-
/** @readonly */
|
|
98
|
-
reduce,
|
|
99
|
-
/** @readonly */
|
|
100
|
-
sum,
|
|
101
|
-
/** @readonly */
|
|
102
|
-
length,
|
|
103
|
-
/** @readonly */
|
|
104
|
-
join,
|
|
105
|
-
/** @readonly */
|
|
106
|
-
scan,
|
|
107
|
-
/** @readonly */
|
|
108
|
-
exclusiveScan,
|
|
109
|
-
/** @readonly */
|
|
110
|
-
takeWhile,
|
|
111
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
const { map, filter, flatMap, sum } = require('.')
|
|
2
|
-
|
|
3
|
-
const test = async () => {
|
|
4
|
-
{
|
|
5
|
-
const a = [1, 2, 3]
|
|
6
|
-
const m = map(x => x * x)(a)
|
|
7
|
-
const result = await sum(m)
|
|
8
|
-
if (result !== 14) { throw 'filter' }
|
|
9
|
-
}
|
|
10
|
-
{
|
|
11
|
-
const a = [1, 2, 3, 4]
|
|
12
|
-
const f = filter(x => x !== 2)(a)
|
|
13
|
-
const result = await sum(f)
|
|
14
|
-
if (result !== 8) { throw 'filter' }
|
|
15
|
-
}
|
|
16
|
-
{
|
|
17
|
-
const a = [1, 2]
|
|
18
|
-
const f = flatMap(x => [x, x * x])(a)
|
|
19
|
-
const result = await sum(f)
|
|
20
|
-
if (result !== 8) { throw 'filter' }
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
test()
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
const { compose } = require('../../function')
|
|
2
|
-
const seq = require('../operator')
|
|
3
|
-
|
|
4
|
-
/** @type {<T>(a: Iterable<T>) => (b: Iterable<T>) => Iterable<T>} */
|
|
5
|
-
const concat = a => b => ({
|
|
6
|
-
*[Symbol.iterator]() {
|
|
7
|
-
yield* a
|
|
8
|
-
yield* b
|
|
9
|
-
}
|
|
10
|
-
})
|
|
11
|
-
|
|
12
|
-
/** @type {<T, R>(s: seq.Scan<T, R>) => (c: Iterable<T>) => Iterable<R>} */
|
|
13
|
-
const scan = s => c => ({
|
|
14
|
-
*[Symbol.iterator]() {
|
|
15
|
-
let next = s
|
|
16
|
-
for (const i of c) {
|
|
17
|
-
const result = next(i)
|
|
18
|
-
next = result[1]
|
|
19
|
-
yield result[0]
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
/** @type {<T, R>(s: seq.ExclusiveScan<T, R>) => (c: Iterable<T>) => Iterable<R>} */
|
|
25
|
-
const exclusiveScan = ([first, s]) => c => concat([first])(scan(s)(c))
|
|
26
|
-
|
|
27
|
-
/** @type {<T, R>(s: seq.ExclusiveScan<T, R>) => (c: Iterable<T>) => R} */
|
|
28
|
-
const reduce = ([first, s]) => c => {
|
|
29
|
-
let next = first
|
|
30
|
-
for (const i of scan(s)(c)) {
|
|
31
|
-
next = i
|
|
32
|
-
}
|
|
33
|
-
return next
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
const entries = scan(seq.entries)
|
|
37
|
-
|
|
38
|
-
const sum = reduce(seq.sum)
|
|
39
|
-
|
|
40
|
-
const length = reduce(seq.length)
|
|
41
|
-
|
|
42
|
-
const join = compose(seq.join)(reduce)
|
|
43
|
-
|
|
44
|
-
/** @type {<T, R>(f: (value: T) => Iterable<R>) => (c: Iterable<T>) => Iterable<R>} */
|
|
45
|
-
const flatMap = f => c => ({
|
|
46
|
-
*[Symbol.iterator]() {
|
|
47
|
-
for (const i of c) {
|
|
48
|
-
yield* f(i)
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
})
|
|
52
|
-
|
|
53
|
-
/** @type {<T, R>(f: (value: T) => R) => (c: Iterable<T>) => Iterable<R>} */
|
|
54
|
-
const map = seq.map(flatMap)
|
|
55
|
-
|
|
56
|
-
/** @type {<T>(f: (value: T) => boolean) => (c: Iterable<T>) => Iterable<T>} */
|
|
57
|
-
const filter = seq.filter(flatMap)
|
|
58
|
-
|
|
59
|
-
/** @type {<T>(c: Iterable<Iterable<T>>) => Iterable<T>} */
|
|
60
|
-
const flat = flatMap(x => x)
|
|
61
|
-
|
|
62
|
-
/** @type {<T>(f: (value: T) => boolean) => (c: Iterable<T>) => Iterable<T>} */
|
|
63
|
-
const takeWhile = f => c => ({
|
|
64
|
-
*[Symbol.iterator]() {
|
|
65
|
-
for (const i of c) {
|
|
66
|
-
if (!f(i)) { return }
|
|
67
|
-
yield i
|
|
68
|
-
}
|
|
69
|
-
}
|
|
70
|
-
})
|
|
71
|
-
|
|
72
|
-
/** @type {<T>(_: (_: T) => boolean) => (_: Iterable<T>) => T|undefined} */
|
|
73
|
-
const find = f => c => {
|
|
74
|
-
for (const i of c) {
|
|
75
|
-
if (f(i)) {
|
|
76
|
-
return i
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
return undefined
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
module.exports = {
|
|
83
|
-
/** @readonly */
|
|
84
|
-
reduce,
|
|
85
|
-
/** @readonly */
|
|
86
|
-
join,
|
|
87
|
-
/** @readonly */
|
|
88
|
-
sum,
|
|
89
|
-
/** @readonly */
|
|
90
|
-
length,
|
|
91
|
-
/** @readonly */
|
|
92
|
-
entries,
|
|
93
|
-
/** @readonly */
|
|
94
|
-
scan,
|
|
95
|
-
/** @readonly */
|
|
96
|
-
exclusiveScan,
|
|
97
|
-
/** @readonly */
|
|
98
|
-
flatMap,
|
|
99
|
-
/** @readonly */
|
|
100
|
-
map,
|
|
101
|
-
/** @readonly */
|
|
102
|
-
filter,
|
|
103
|
-
/** @readonly */
|
|
104
|
-
flat,
|
|
105
|
-
/** @readonly */
|
|
106
|
-
takeWhile,
|
|
107
|
-
/** @readonly */
|
|
108
|
-
find,
|
|
109
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
const i = require('.')
|
|
2
|
-
const { compose } = require('../../function')
|
|
3
|
-
|
|
4
|
-
{
|
|
5
|
-
const r = i.sum([120, 300, 42])
|
|
6
|
-
if (r !== 462) { throw 'error' }
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
{
|
|
10
|
-
if (i.sum([1, 2]) !== 3) { throw 'error' }
|
|
11
|
-
if (i.sum([1, 2]) !== 3) { throw 'error' }
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
{
|
|
15
|
-
const x = Array.from(i.map(a => a ** 2)([1, 2, 3]))
|
|
16
|
-
if (x.length !== 3) { throw 'error' }
|
|
17
|
-
if (x[0] !== 1) { throw 'error' }
|
|
18
|
-
if (x[1] !== 4) { throw 'error' }
|
|
19
|
-
if (x[2] !== 9) { throw 'error' }
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
{
|
|
23
|
-
if (i.join('/')([]) !== '') { throw 'error' }
|
|
24
|
-
if (i.join('/')(['a']) !== 'a') { throw 'error' }
|
|
25
|
-
if (i.join('/')(['a', 'b']) !== 'a/b') { throw 'error'}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
{
|
|
29
|
-
if (i.find(x => x === 'c')(['a', 'b', 'c']) !== 'c') { throw 'error' }
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
{
|
|
33
|
-
/** @type {(_: string) => string|undefined} */
|
|
34
|
-
const file = _ => 'x'
|
|
35
|
-
/** @type {(_: string) => string|undefined} */
|
|
36
|
-
const x = p => compose
|
|
37
|
-
(i.map(x => file(x())))
|
|
38
|
-
(i.find(x => x !== undefined))
|
|
39
|
-
([() => p, () => `${p}.js`, () => `${p}/index.js`])
|
|
40
|
-
if (x('index.js') !== 'x') { throw 'error' }
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
{
|
|
44
|
-
const x = JSON.stringify(Array.from(i.entries(['a', 'b', 'c'])))
|
|
45
|
-
if (x !== '[[0,"a"],[1,"b"],[2,"c"]]') { throw x }
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
{
|
|
49
|
-
const x = JSON.stringify(Array.from(i.entries([])))
|
|
50
|
-
if (x !== '[]') { throw x }
|
|
51
|
-
}
|
|
@@ -1,92 +0,0 @@
|
|
|
1
|
-
const op = require('../../function/operator')
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @template T0
|
|
5
|
-
* @template T1
|
|
6
|
-
* @typedef {import('../array').Tuple2<T0, T1>} Tuple2
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
/**
|
|
10
|
-
* @template T
|
|
11
|
-
* @template R
|
|
12
|
-
* @typedef {Tuple2<R, Scan<T, R>>} ScanResult
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* @template T
|
|
17
|
-
* @template R
|
|
18
|
-
* @typedef {(value: T) => ScanResult<T, R>} Scan
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* @template T
|
|
23
|
-
* @template R
|
|
24
|
-
* @typedef {Tuple2<R, Scan<T, R>>} ExclusiveScan
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
/** @type {<R, T>(operator: op.ReduceOperator<R, T>) => (prior: R) => Scan<T, R>} */
|
|
28
|
-
const scan = operator => {
|
|
29
|
-
/** @typedef {typeof operator extends op.ReduceOperator<infer R, infer T> ? [R, T] : never} RT */
|
|
30
|
-
/** @typedef {RT[0]} R */
|
|
31
|
-
/** @typedef {RT[1]} T */
|
|
32
|
-
/** @type {(prior: R) => Scan<T, R>} */
|
|
33
|
-
const f = prior => value => {
|
|
34
|
-
const result = operator(prior)(value)
|
|
35
|
-
return [result, f(result)]
|
|
36
|
-
}
|
|
37
|
-
return f
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/** @type {<R, T>(operator: op.ReduceOperator<R, T>) => (first: R) => ExclusiveScan<T, R>} */
|
|
41
|
-
const exclusiveScan = operator => first => [first, scan(operator)(first)]
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* @template T
|
|
45
|
-
* @typedef {Tuple2<number, T>} Entry
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
|
-
/** @type {(index: number) => <T>(value: T) => ScanResult<T, Entry<T>>} */
|
|
49
|
-
const createEntries = index => value => [[index, value], createEntries(index + 1)]
|
|
50
|
-
|
|
51
|
-
const entries = createEntries(0)
|
|
52
|
-
|
|
53
|
-
/** @type {(separator: string) => ExclusiveScan<string, string>} */
|
|
54
|
-
const join = separator => ['', value => [value, scan(op.join(separator))(value)]]
|
|
55
|
-
|
|
56
|
-
const sum = exclusiveScan(op.addition)(0)
|
|
57
|
-
|
|
58
|
-
/** @type {(a: number) => () => number} */
|
|
59
|
-
const counter = a => () => a + 1
|
|
60
|
-
|
|
61
|
-
const length = exclusiveScan(counter)(0)
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @template T
|
|
65
|
-
* @template R
|
|
66
|
-
* @typedef {(value: T) => R} Func
|
|
67
|
-
*/
|
|
68
|
-
|
|
69
|
-
/** @type {<T, R, X>(flatMap: (f: Func<T, readonly[R]>) => X) => (f: Func<T, R>) =>X} */
|
|
70
|
-
const map = flatMap => f => flatMap(x => [f(x)])
|
|
71
|
-
|
|
72
|
-
/** @type {<T, X>(flatMap: (f: Func<T, readonly[T]|[]>) => X) => (f: Func<T, boolean>) =>X} */
|
|
73
|
-
const filter = flatMap => f => flatMap(x => f(x) ? [x] : [])
|
|
74
|
-
|
|
75
|
-
module.exports = {
|
|
76
|
-
/** @readonly */
|
|
77
|
-
exclusiveScan,
|
|
78
|
-
/** @readonly */
|
|
79
|
-
scan,
|
|
80
|
-
/** @readonly */
|
|
81
|
-
join,
|
|
82
|
-
/** @readonly */
|
|
83
|
-
sum,
|
|
84
|
-
/** @readonly */
|
|
85
|
-
length,
|
|
86
|
-
/** @readonly */
|
|
87
|
-
entries,
|
|
88
|
-
/** @readonly */
|
|
89
|
-
map,
|
|
90
|
-
/** @readonly */
|
|
91
|
-
filter,
|
|
92
|
-
}
|