functionalscript 0.0.155 → 0.0.156

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.
File without changes
package/async/index.js ADDED
@@ -0,0 +1,101 @@
1
+ const { pipe } = require('../lib')
2
+
3
+ /** @type {<T, R>(f: (value: T) => Promise<R>) => (c: AsyncIterable<T>) => AsyncIterable<R>} */
4
+ const map = f => c => ({
5
+ async *[Symbol.asyncIterator]() {
6
+ for await (const i of c) {
7
+ yield f(i)
8
+ }
9
+ }
10
+ })
11
+
12
+ /** @type {<T>(c: AsyncIterable<AsyncIterable<T>>) => AsyncIterable<T>} */
13
+ const flatten = c => ({
14
+ async *[Symbol.asyncIterator]() {
15
+ for await (const i of c) {
16
+ yield *i
17
+ }
18
+ }
19
+ })
20
+
21
+ /** @type {<T>(f: (value: T) => Promise<boolean>) => (c: AsyncIterable<T>) => AsyncIterable<T>} */
22
+ const filter = f => c => ({
23
+ async *[Symbol.asyncIterator]() {
24
+ for await (const i of c) {
25
+ if (await f(i)) {
26
+ yield i
27
+ }
28
+ }
29
+ }
30
+ })
31
+
32
+ /** @type {<T, R>(f: (value: T) => AsyncIterable<R>) => (c: AsyncIterable<T>) => AsyncIterable<R>} */
33
+ const flatMap = f => pipe(map(async x => f(x)))(flatten)
34
+
35
+ /**
36
+ * @template A
37
+ * @template T
38
+ * @typedef {(accumulator: A) => (value: T) => Promise<A>} ReduceFn
39
+ */
40
+
41
+ /** @type {<A, T>(f: ReduceFn<A, T>) => (init: A) => (c: AsyncIterable<T>) => Promise<A>} */
42
+ const reduce = reduceFn => init => async c => {
43
+ let result = init
44
+ for await (const i of c) {
45
+ result = await reduceFn(result)(i)
46
+ }
47
+ return result
48
+ }
49
+
50
+ /** @type {<A, T>(f: ReduceFn<A, T>) => (init: A) => (c: AsyncIterable<T>) => AsyncIterable<A>} */
51
+ const exclusiveScan = reduceFn => init => c => ({
52
+ async *[Symbol.asyncIterator]() {
53
+ let result = init
54
+ for await (const i of c) {
55
+ result = await reduceFn(result)(i)
56
+ yield result
57
+ }
58
+ }
59
+ })
60
+
61
+ /** @type {<A, T>(f: ReduceFn<A, T>) => (init: A) => (c: AsyncIterable<T>) => AsyncIterable<A>} */
62
+ const inclusiveScan = reduceFn => init => {
63
+ const e = exclusiveScan(reduceFn)(init)
64
+ return c => ({
65
+ async *[Symbol.asyncIterator]() {
66
+ yield init
67
+ yield *e(c)
68
+ }
69
+ })
70
+ }
71
+
72
+ /** @type {<T>(iterable: Iterable<T>) => AsyncIterable<T>} */
73
+ const cast = iterable => ({
74
+ async *[Symbol.asyncIterator]() {
75
+ for (const i of iterable) {
76
+ yield i
77
+ }
78
+ }
79
+ })
80
+
81
+ /** @type {(c: AsyncIterable<number>) => Promise<number>} */
82
+ const sum = reduce(a => async v => a + v)(0)
83
+
84
+ module.exports = {
85
+ /** @readonly */
86
+ cast,
87
+ /** @readonly */
88
+ map,
89
+ /** @readonly */
90
+ filter,
91
+ /** @readonly */
92
+ flatMap,
93
+ /** @readonly */
94
+ reduce,
95
+ /** @readonly */
96
+ sum,
97
+ /** @readonly */
98
+ exclusiveScan,
99
+ /** @readonly */
100
+ inclusiveScan,
101
+ }
package/async/test.js ADDED
@@ -0,0 +1,24 @@
1
+ const { cast, map, filter, flatMap, sum } = require('.')
2
+
3
+ const test = async () => {
4
+ {
5
+ const a = cast([1, 2, 3])
6
+ const m = map(async x => x * x)(a)
7
+ const result = await sum(m)
8
+ if (result !== 14) { throw 'filter' }
9
+ }
10
+ {
11
+ const a = cast([1, 2, 3, 4])
12
+ const f = filter(async x => x !== 2)(a)
13
+ const result = await sum(f)
14
+ if (result !== 8) { throw 'filter' }
15
+ }
16
+ {
17
+ const a = cast([1, 2])
18
+ const f = flatMap(x => cast([x, x * x]))(a)
19
+ const result = await sum(f)
20
+ if (result !== 8) { throw 'filter' }
21
+ }
22
+ }
23
+
24
+ test()
File without changes
@@ -1,35 +1,35 @@
1
- const { index3, index5 } = require('./cmp')
1
+ const { index3, index5 } = require('../cmp')
2
2
 
3
3
  /**
4
4
  * @template T
5
- * @typedef {import('./lazy').Lazy<T>} Lazy
5
+ * @typedef {() => T} Lazy
6
6
  */
7
7
 
8
8
  /**
9
9
  * @template T
10
- * @typedef {import('./cmp').Cmp<T>} Cmp
10
+ * @typedef {import('../cmp').Cmp<T>} Cmp
11
11
  */
12
12
 
13
13
  /**
14
14
  * @template T
15
- * @typedef {import('./array').Array1<T>} Array1
15
+ * @typedef {import('../array').Array1<T>} Array1
16
16
  */
17
17
 
18
18
  /**
19
19
  * @template T
20
- * @typedef {import('./array').Array2<T>} Array2
20
+ * @typedef {import('../array').Array2<T>} Array2
21
21
  */
22
22
 
23
23
  /**
24
24
  * @template T
25
- * @typedef {import('./array').Array3<T>} Array3
25
+ * @typedef {import('../array').Array3<T>} Array3
26
26
  */
27
27
 
28
- /** @typedef {import('./array').Index2} Index2 */
28
+ /** @typedef {import('../array').Index2} Index2 */
29
29
 
30
- /** @typedef {import('./array').Index3} Index3 */
30
+ /** @typedef {import('../array').Index3} Index3 */
31
31
 
32
- /** @typedef {import('./array').Index5} Index5 */
32
+ /** @typedef {import('../array').Index5} Index5 */
33
33
 
34
34
  //
35
35
 
@@ -1,9 +1,9 @@
1
- /** @typedef {import('./array').Index3} Index3 */
2
- /** @typedef {import('./array').Index5} Index5 */
1
+ /** @typedef {import('../array').Index3} Index3 */
2
+ /** @typedef {import('../array').Index5} Index5 */
3
3
 
4
4
  /**
5
5
  * @template T
6
- * @typedef {import('./array').Array2<T>} Array2
6
+ * @typedef {import('../array').Array2<T>} Array2
7
7
  */
8
8
 
9
9
  /** @typedef {-1|0|1} Sign */
@@ -1,7 +1,9 @@
1
- const lib = require('../')
1
+ const { pipe } = require('../lib')
2
+ const lib = require('../lib')
3
+ const mr = require('../map-reduce')
2
4
 
3
- /** @type {<T, R>(_: lib.Reduce<T, R>) => (_: Iterable<T>) => R} */
4
- const reduce = ({ merge, init }) => c => {
5
+ /** @type {<T, R>(merge: (_: R) => (_: T) => R) => (init: R) => (_: Iterable<T>) => R} */
6
+ const reduce = merge => init => c => {
5
7
  let result = init
6
8
  for (const i of c) {
7
9
  result = merge(result)(i)
@@ -9,15 +11,21 @@ const reduce = ({ merge, init }) => c => {
9
11
  return result
10
12
  }
11
13
 
14
+ /** @type {<I, S, R>(op: mr.Operation<I, S, R>) => (_: Iterable<I>) => R} */
15
+ const apply = op => pipe(reduce(op.reduce)(op.init))(op.result)
16
+
12
17
  module.exports = {
18
+ /** @readonly */
19
+ apply,
20
+
13
21
  /** @readonly */
14
22
  reduce,
15
23
 
16
24
  /** @readonly */
17
- join: lib.pipe(lib.join)(reduce),
25
+ join: pipe(mr.join)(apply),
18
26
 
19
27
  /** @readonly */
20
- sum: reduce(lib.sum),
28
+ sum: apply(mr.sum),
21
29
 
22
30
  /**
23
31
  * @readonly
@@ -31,18 +39,6 @@ module.exports = {
31
39
  }
32
40
  }),
33
41
 
34
- /**
35
- * @readonly
36
- * @type {<T, R>(f: (value: T) => Promise<R>) => (c: AsyncIterable<T>) => AsyncIterable<R>}
37
- */
38
- asyncMap: f => c => ({
39
- async *[Symbol.asyncIterator]() {
40
- for await (const i of c) {
41
- yield* [f(i)]
42
- }
43
- }
44
- }),
45
-
46
42
  /**
47
43
  * @readonly
48
44
  * @type {<T>(_: (_: T) => boolean) => (_: Iterable<T>) => T|undefined}
@@ -1,5 +1,5 @@
1
1
  const i = require('.')
2
- const lib = require('..')
2
+ const lib = require('../lib')
3
3
 
4
4
  {
5
5
  const r = i.sum([120, 300, 42])
package/lib/index.js CHANGED
@@ -28,19 +28,6 @@
28
28
  /** @type {(_: string) => never} */
29
29
  const panic = message => { throw message }
30
30
 
31
- /** @type {<A>(_: A) => <B>(_: B) => [A, B]} */
32
- const tuple2 = a => b => [a, b]
33
-
34
- // /** @type {<T>(_: (_: string) => T) => (_: Dictionary<T>) => (_: string) => [T, Dictionary<T>]} */
35
- // const cache = f => d => k => {
36
- // const set = () => {
37
- // const r = f(k)
38
- // return tuple2(r)(d.set(k)(r))
39
- // }
40
- // const result = d.get(k)
41
- // return result !== undefined ? [result, d] : set()
42
- // }
43
-
44
31
  /**
45
32
  * @template T
46
33
  * @template R
@@ -88,12 +75,6 @@ const chain = value => ({
88
75
  * }} Pipe
89
76
  */
90
77
 
91
- /** @type {<I, T>(f: (input: I) => T) => Pipe<I, T>} */
92
- const pipex = f => ({
93
- x: g => pipex(pipe(f)(g)),
94
- f,
95
- })
96
-
97
78
  module.exports = {
98
79
 
99
80
  panic,
@@ -113,22 +94,8 @@ module.exports = {
113
94
  }
114
95
  },
115
96
 
116
- /** @type {(s: string) => Reduce<string, string>} */
117
- join: s => ({ merge: a => i => a === '' ? i : a + s + i, init: ''}),
118
-
119
97
  pipe,
120
98
 
121
- pipex,
122
-
123
- /** @type {Reduce<number, number>} */
124
- sum: { merge: a => i => a + i, init: 0 },
125
-
126
- /** @type {<I, X>(f: (init: I) => X) => <O>(reduce: Reduce<X, O>) => Reduce<I, O>} */
127
- map: f => ({merge, init}) => ({
128
- merge: pipe(merge)(pipe(f)),
129
- init,
130
- }),
131
-
132
99
  last,
133
100
 
134
101
  optionMap,
package/lib/test.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const lib = require('./index')
2
2
 
3
- require('./iterable/test')
4
- require('./map/test')
3
+ require('../iterable/test')
4
+ require('../map/test')
5
5
 
@@ -1,21 +1,21 @@
1
- const { optionMap } = require("..")
2
- const { getVisitor, setVisitor, values } = require("../tree")
1
+ const { optionMap } = require("../lib")
2
+ const { getVisitor, setVisitor, values } = require("../btree")
3
3
 
4
- /** @typedef {import("../tree/cmp").Sign} Sign */
4
+ /** @typedef {import("../cmp").Sign} Sign */
5
5
 
6
6
  /**
7
7
  * @template T
8
- * @typedef {import("../tree").Leaf1<T>} Leaf1
8
+ * @typedef {import("../btree").Leaf1<T>} Leaf1
9
9
  */
10
10
 
11
11
  /**
12
12
  * @template T
13
- * @typedef {import("../tree").TNode<T>} TNode
13
+ * @typedef {import("../btree").TNode<T>} TNode
14
14
  */
15
15
 
16
16
  /**
17
17
  * @template T
18
- * @typedef {import("../tree/cmp").Cmp<T>} Cmp
18
+ * @typedef {import("../cmp").Cmp<T>} Cmp
19
19
  */
20
20
 
21
21
  /**
@@ -1,5 +1,5 @@
1
1
  const { empty } = require('.')
2
- const lib = require('..')
2
+ const lib = require('../lib')
3
3
 
4
4
  {
5
5
  let m = empty.set('a')(1)
@@ -53,7 +53,7 @@ const lib = require('..')
53
53
  {
54
54
  /** @type {import('.').Map<number>} */
55
55
  let m = empty
56
- for (let i = 0; i < 1_000_000; ++i) {
56
+ for (let i = 0; i < 100_000; ++i) {
57
57
  m = m.set((i*i).toString())(i)
58
58
  /*
59
59
  console.log()
@@ -0,0 +1,42 @@
1
+ const { pipe } = require('../lib')
2
+
3
+ /**
4
+ * @template I
5
+ * @template S
6
+ * @template O
7
+ * @typedef {{
8
+ * readonly reduce: (state: S) => (value: I) => S
9
+ * readonly result: (state: S) => O
10
+ * readonly init: S
11
+ * }} Operation
12
+ */
13
+
14
+ /** @type {<I, T>(mapFn: (value: I) => T) => <S, O>(op: Operation<T, S, O>) => Operation<I, S, O>} */
15
+ const map = mapFn => ({ reduce, result, init}) => ({
16
+ reduce: pipe(reduce)(pipe(mapFn)),
17
+ result,
18
+ init,
19
+ })
20
+
21
+ /** @type {(separator: string) => Operation<string, string|undefined, string>} */
22
+ const join = separator => ({
23
+ reduce: s => i => s === undefined ? i : `${s}${separator}${i}`,
24
+ init: undefined,
25
+ result: s => s === undefined ? '' : s
26
+ })
27
+
28
+ /** @type {Operation<number, number, number>} */
29
+ const sum = {
30
+ reduce: a => i => a + i,
31
+ result: a => a,
32
+ init: 0,
33
+ }
34
+
35
+ module.exports = {
36
+ /** @readonly */
37
+ map,
38
+ /** @readonly */
39
+ join,
40
+ /** @readonly */
41
+ sum,
42
+ }
@@ -1,5 +1,6 @@
1
1
  const lib = require('../lib')
2
- const iter = require('../lib/iterable')
2
+ const iter = require('../iterable')
3
+ const mr = require('../map-reduce')
3
4
 
4
5
  /** @typedef {(_: string) => string|undefined} ReadFile */
5
6
 
@@ -28,9 +29,9 @@ const iter = require('../lib/iterable')
28
29
 
29
30
  /** @typedef {(_: string) => undefined|Package|Dependencies} Dependencies */
30
31
 
31
- /** @type {lib.Reduce<string, undefined|string[]>} */
32
+ /** @type {mr.Operation<string, undefined|string[], undefined|string[]>} */
32
33
  const pathNormReduce = {
33
- merge: path => item =>
34
+ reduce: path => item =>
34
35
  path === undefined ?
35
36
  undefined :
36
37
  ['', '.'].includes(item) ?
@@ -38,13 +39,14 @@ const pathNormReduce = {
38
39
  item === '..' ?
39
40
  lib.head(path) :
40
41
  [...path, item],
41
- init: []
42
+ init: [],
43
+ result: s => s,
42
44
  }
43
45
 
44
46
  /** @type {(_: string[]) => boolean} */
45
47
  const isRelative = localId => ['.', '..'].includes(localId[0])
46
48
 
47
- const pathNorm = iter.reduce(pathNormReduce)
49
+ const pathNorm = iter.apply(pathNormReduce)
48
50
 
49
51
  /** @type {(_: Package) => (_: string[]) => Module|undefined} */
50
52
  const internal = pack => {
@@ -91,8 +93,12 @@ const getModule = ({pack, local}) => path => {
91
93
  const moduleId = module => [...module.location.pack.id, ...module.location.local, module.fileName].join('/')
92
94
 
93
95
  module.exports = {
96
+ /** @readonly */
94
97
  isRelative,
98
+ /** @readonly */
95
99
  pathNorm,
100
+ /** @readonly */
96
101
  getModule,
102
+ /** @readonly */
97
103
  moduleId,
98
104
  }
@@ -2,12 +2,6 @@ const i = require('.')
2
2
 
3
3
  const lib = require('../lib')
4
4
 
5
- {
6
- if (require('./test0') !== 'test0.js') { throw './test0' }
7
- if (require('./test0.js') !== 'test0.js') { throw './test0.js'}
8
- if (require('./test0.js.js') !== 'test0.js.js') { throw './test0.js.js'}
9
- }
10
-
11
5
  require('./node/test')
12
6
 
13
7
  /** @type {<T>(_: T | undefined) => T} */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.155",
3
+ "version": "0.0.156",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/test.js CHANGED
@@ -1,6 +1,7 @@
1
1
  const lib = require('./lib')
2
2
  const i = require('./')
3
3
 
4
+ require('./async/test')
4
5
  require('./module-manager/test')
5
6
  require('./lib/test')
6
7
 
package/lib/tree/lazy.js DELETED
@@ -1,6 +0,0 @@
1
- /**
2
- * @template T
3
- * @typedef {() => T} Lazy
4
- */
5
-
6
- module.exports = {}
@@ -1 +0,0 @@
1
- module.exports = 'test0.js'
@@ -1 +0,0 @@
1
- module.exports = 'test0.js.js'