functionalscript 0.0.396 → 0.0.400

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.
@@ -3,7 +3,7 @@ const module_ = require('../module/module.f.cjs')
3
3
  const { idToString, dir } = module_
4
4
  const function_ = require('../module/function/module.f.cjs')
5
5
  const map = require('../../types/map/module.f.cjs')
6
- const { set: mapSet } = map
6
+ const { setReplace } = map
7
7
  const object = require('../../types/object/module.f.cjs')
8
8
  const { fromMap } = object
9
9
  const path = require('../path/module.f.cjs')
@@ -62,11 +62,11 @@ const getOrBuild = compile => packageGet => moduleMapInterface => {
62
62
  const rIdStr = idToString(r.id)
63
63
  if (setContains(rIdStr)(buildSet1)) { return error('circular reference') }
64
64
  const [state, m1] = build(buildSet1)(r.id)(r.source)(m)
65
- return [state[0] === 'error' ? state : ['ok', state[1].exports], [mapSet(p)(rIdStr)(requireMap), m1]]
65
+ return [state[0] === 'error' ? state : ['ok', state[1].exports], [setReplace(p)(rIdStr)(requireMap), m1]]
66
66
  }
67
67
  return source => moduleMap => {
68
68
  /** @type {(s: module_.State) => (m: M) => Result<M>} */
69
- const set = s => m => [s, moduleMapInterface.set(moduleIdStr)(s)(m)]
69
+ const set = s => m => [s, moduleMapInterface.setReplace(moduleIdStr)(s)(m)]
70
70
  /** @type {(e: module_.Error) => (m: M) => Result<M>} */
71
71
  const error = e => set(['error', e])
72
72
  // check compilation
@@ -4,7 +4,7 @@ const object = require('../../types/object/module.f.cjs')
4
4
  * @template M
5
5
  * @typedef {{
6
6
  * readonly at: (moduleId: string) => (moduleMap: M) => State | undefined
7
- * readonly set: (moduleId: string) => (moduleState: State) => (moduleMap: M) => M
7
+ * readonly setReplace: (moduleId: string) => (moduleState: State) => (moduleMap: M) => M
8
8
  * }} MapInterface
9
9
  */
10
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.396",
3
+ "version": "0.0.400",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "module.f.cjs",
6
6
  "scripts": {
@@ -29,7 +29,7 @@
29
29
  },
30
30
  "homepage": "https://github.com/functionalscript/functionalscript#readme",
31
31
  "devDependencies": {
32
- "@types/node": "^18.6.3",
32
+ "@types/node": "^18.7.8",
33
33
  "typescript": "^4.7.4"
34
34
  }
35
35
  }
@@ -6,5 +6,8 @@ const addition = a => b => a + b
6
6
  const sum = fold(addition)(0n)
7
7
 
8
8
  module.exports = {
9
+ /** @readonly */
10
+ addition,
11
+ /** @readonly */
9
12
  sum,
10
13
  }
@@ -3,19 +3,19 @@ const list = require('../../list/module.f.cjs')
3
3
  const json = require('../../../json/module.f.cjs')
4
4
  const { sort } = require('../../object/module.f.cjs')
5
5
  const btree = require('../types/module.f.cjs')
6
- const { stringCmp } = require('../../function/compare/module.f.cjs')
6
+ const { cmp } = require('../../string/module.f.cjs')
7
7
  const s = require('../set/module.f.cjs')
8
8
 
9
9
  const jsonStr = json.stringify(sort)
10
10
 
11
11
  /** @type {(node: btree.Node<string>) => (value: string) => btree.Node<string>} */
12
- const set = node => value => s.set(stringCmp(value))(value)(node)
12
+ const set = node => value => s.set(cmp(value))(() => value)(node)
13
13
 
14
14
  /** @type {(r: _.Result<json.Unknown>) => string} */
15
15
  const str = r => jsonStr(list.toArray(list.map(x => x[0])(r)))
16
16
 
17
17
  /** @type {(i: string) => (m: btree.Node<string>) => string} */
18
- const find = i => m => str(_.find(stringCmp(i))(m))
18
+ const find = i => m => str(_.find(cmp(i))(m))
19
19
 
20
20
  {
21
21
  /** @type {btree.Node<string>} */
@@ -1,15 +1,15 @@
1
1
  const _ = require('./module.f.cjs')
2
2
  const btree = require('../types/module.f.cjs')
3
3
  const s = require('../set/module.f.cjs')
4
- const { stringCmp } = require('../../function/compare/module.f.cjs')
4
+ const { cmp } = require('../../string/module.f.cjs')
5
5
  const json = require('../../../json/module.f.cjs')
6
6
  const { sort } = require('../../object/module.f.cjs')
7
7
 
8
8
  /** @type {(node: btree.Node<string>) => (value: string) => btree.Node<string>} */
9
- const set = node => value => s.set(stringCmp(value))(value)(node)
9
+ const set = node => value => s.set(cmp(value))(() => value)(node)
10
10
 
11
11
  /** @type {(node: btree.Node<string>) => (value: string) => btree.Node<string> | undefined} */
12
- const remove = node => value => _.nodeRemove(stringCmp(value))(node)
12
+ const remove = node => value => _.nodeRemove(cmp(value))(node)
13
13
 
14
14
  const jsonStr = json.stringify(sort)
15
15
 
@@ -36,16 +36,17 @@ const reduceOp = ([i, x]) => a => {
36
36
 
37
37
  const reduceBranch = reduce(reduceOp)
38
38
 
39
- /** @type {<T>(c: cmp.Compare<T>) => (value: T) => (node: _.Node<T>) => _.Node<T>} */
40
- const nodeSet = c => value => node => {
39
+ /** @type {<T>(c: cmp.Compare<T>) => (g: (value?: T) => T) => (node: _.Node<T>) => _.Node<T>} */
40
+ const nodeSet = c => g => node => {
41
41
  const { first, tail } = find(c)(node)
42
42
  const [i, x] = first;
43
- /** @typedef {typeof value} T */
43
+ /** @typedef {typeof c extends cmp.Compare<infer T> ? T : never} T */
44
44
  /** @type {() => Branch1To3<T>} */
45
45
  const f = () => {
46
46
  switch (i) {
47
47
  case 0: {
48
48
  // insert
49
+ const value = g()
49
50
  switch (x.length) {
50
51
  case 1: { return [[value, x[0]]] }
51
52
  case 2: { return [[value], x[0], [x[1]]] }
@@ -54,14 +55,15 @@ const nodeSet = c => value => node => {
54
55
  case 1: {
55
56
  // replace
56
57
  switch (x.length) {
57
- case 1: { return [[value]] }
58
- case 2: { return [[value, x[1]]] }
59
- case 3: { return [[x[0], value, x[2]]] }
60
- case 5: { return [[x[0], value, x[2], x[3], x[4]]] }
58
+ case 1: { return [[g(x[0])]] }
59
+ case 2: { return [[g(x[0]), x[1]]] }
60
+ case 3: { return [[x[0], g(x[1]), x[2]]] }
61
+ case 5: { return [[x[0], g(x[1]), x[2], x[3], x[4]]] }
61
62
  }
62
63
  }
63
64
  case 2: {
64
65
  // insert
66
+ const value = g()
65
67
  switch (x.length) {
66
68
  case 1: { return [[x[0], value]] }
67
69
  case 2: { return [[x[0]], value, [x[1]]] }
@@ -70,14 +72,14 @@ const nodeSet = c => value => node => {
70
72
  case 3: {
71
73
  // replace
72
74
  switch (x.length) {
73
- case 2: { return [[x[0], value]] }
74
- case 5: { return [[x[0], x[1], x[2], value, x[4]]]}
75
+ case 2: { return [[x[0], g(x[1])]] }
76
+ case 5: { return [[x[0], x[1], x[2], g(x[3]), x[4]]]}
75
77
  }
76
78
  }
77
79
  case 4: {
78
80
  // insert
79
81
  const [v0, v1] = x;
80
- return [[v0], v1, [value]]
82
+ return [[v0], v1, [g()]]
81
83
  }
82
84
  }
83
85
  }
@@ -85,8 +87,8 @@ const nodeSet = c => value => node => {
85
87
  return r.length === 1 ? r[0] : r
86
88
  }
87
89
 
88
- /** @type {<T>(c: cmp.Compare<T>) => (value: T) => (tree: _.Tree<T>) => _.Node<T>} */
89
- const set = c => value => tree => tree === undefined ? [value] : nodeSet(c)(value)(tree)
90
+ /** @type {<T>(c: cmp.Compare<T>) => (f: (value?: T) => T) => (tree: _.Tree<T>) => _.Node<T>} */
91
+ const set = c => f => tree => tree === undefined ? [f()] : nodeSet(c)(f)(tree)
90
92
 
91
93
  module.exports = {
92
94
  /** @readonly */
@@ -1,11 +1,11 @@
1
1
  const _ = require('./module.f.cjs')
2
2
  const btree = require('../types/module.f.cjs')
3
- const { stringCmp } = require('../../function/compare/module.f.cjs')
3
+ const { cmp } = require('../../string/module.f.cjs')
4
4
  const json = require('../../../json/module.f.cjs')
5
5
  const { sort } = require('../../object/module.f.cjs')
6
6
 
7
7
  /** @type {(node: btree.Node<string>) => (value: string) => btree.Node<string>} */
8
- const set = node => value => _.set(stringCmp(value))(value)(node)
8
+ const set = node => value => _.set(cmp(value))(() => value)(node)
9
9
 
10
10
  const jsonStr = json.stringify(sort)
11
11
 
@@ -2,7 +2,7 @@ const btree = require('./types/module.f.cjs')
2
2
  const { values } = require('./module.f.cjs')
3
3
  const json = require('../../json/module.f.cjs')
4
4
  const { sort } = require('../object/module.f.cjs')
5
- const { stringCmp } = require('../function/compare/module.f.cjs')
5
+ const { cmp } = require('../string/module.f.cjs')
6
6
  const list = require('../list/module.f.cjs')
7
7
  const s = require('./set/module.f.cjs')
8
8
  const f = require('./find/module.f.cjs')
@@ -17,7 +17,7 @@ const jsonStr = json.stringify(sort)
17
17
  const stringify = sequence => jsonStr(list.toArray(sequence))
18
18
 
19
19
  /** @type {(node: btree.Node<string>) => (value: string) => btree.Node<string>} */
20
- const set = node => value => s.set(stringCmp(value))(value)(node)
20
+ const set = node => value => s.set(cmp(value))(() => value)(node)
21
21
 
22
22
  {
23
23
  /** @type {btree.Node<string>} */
@@ -45,7 +45,7 @@ const set = node => value => s.set(stringCmp(value))(value)(node)
45
45
  let _map = ['a']
46
46
  _map = set(_map)('b')
47
47
  _map = set(_map)('c')
48
- const result = f.value(f.find(stringCmp('b'))(_map).first)
48
+ const result = f.value(f.find(cmp('b'))(_map).first)
49
49
  if (result !== 'b') { throw result }
50
50
  }
51
51
 
@@ -54,7 +54,7 @@ const set = node => value => s.set(stringCmp(value))(value)(node)
54
54
  let _map = ['a']
55
55
  _map = set(_map)('b')
56
56
  _map = set(_map)('c')
57
- const result = f.value(f.find(stringCmp('e'))(_map).first)
57
+ const result = f.value(f.find(cmp('e'))(_map).first)
58
58
  if (result !== undefined) { throw result }
59
59
  }
60
60
 
@@ -27,19 +27,11 @@ const index5 = cmp => ([v0, v1]) => {
27
27
  /** @type {<T>(a: T) => (b: T) => Sign} */
28
28
  const unsafeCmp = a => b => a < b ? -1 : a === b ? 0 : 1
29
29
 
30
- /** @type {(a: string) => (b: string) => Sign} */
31
- const stringCmp = unsafeCmp
32
-
33
- /** @type {(a: number) => (b: number) => Sign} */
34
- const numberCmp = unsafeCmp
35
-
36
30
  module.exports = {
31
+ /** @readonly */
32
+ unsafeCmp,
37
33
  /** @readonly */
38
34
  index3,
39
35
  /** @readonly */
40
36
  index5,
41
- /** @readonly */
42
- stringCmp,
43
- /** @readonly */
44
- numberCmp,
45
37
  }
@@ -1,13 +1,8 @@
1
- const _ = require('./module.f.cjs')
1
+ const { unsafeCmp } = require('./module.f.cjs')
2
2
 
3
3
  {
4
- const result = _.numberCmp(4)(5)
5
- if (result !== -1) { throw result }
6
- }
7
-
8
- {
9
- const result = _.stringCmp('3')('4')
10
- if (result !== -1) { throw result }
4
+ const result = unsafeCmp(true)(false)
5
+ if (result !== 1) { throw result }
11
6
  }
12
7
 
13
8
  module.exports = {}
@@ -4,10 +4,11 @@ const btreeFind = require('../btree/find/module.f.cjs')
4
4
  const { value, find } = btreeFind
5
5
  const btreeSet = require('../btree/set/module.f.cjs').set
6
6
  const compare = require('../function/compare/module.f.cjs')
7
- const { stringCmp } = require('../function/compare/module.f.cjs')
7
+ const { cmp } = require('../string/module.f.cjs')
8
8
  const list = require('../list/module.f.cjs')
9
9
  const { reduce } = list
10
10
  const { remove: btreeRemove } = require('../btree/remove/module.f.cjs')
11
+ const operator = require('../function/operator/module.f.cjs')
11
12
 
12
13
  /** @typedef {compare.Sign} Sign */
13
14
 
@@ -27,7 +28,7 @@ const { remove: btreeRemove } = require('../btree/remove/module.f.cjs')
27
28
  */
28
29
 
29
30
  /** @type {(a: string) => <T>(b: Entry<T>) => Sign} */
30
- const keyCmp = a => ([b]) => stringCmp(a)(b)
31
+ const keyCmp = a => ([b]) => cmp(a)(b)
31
32
 
32
33
  /** @type {(name: string) => <T>(map: Map<T>) => T|undefined} */
33
34
  const at = name => map => {
@@ -36,17 +37,23 @@ const at = name => map => {
36
37
  return result === undefined ? undefined : result[1]
37
38
  }
38
39
 
39
- /** @type {<T>(entry: Entry<T>) => (map: Map<T>) => Map<T>} */
40
- const setEntry = entry => btreeSet(keyCmp(entry[0]))(entry)
40
+ /** @type {<T>(o: operator.Fold<T>) => (entry: Entry<T>) => (map: Map<T>) => Map<T>} */
41
+ const setUpdateEntry = o => entry => btreeSet(keyCmp(entry[0]))(old => old === undefined ? entry : [old[0], o(old[1])(entry[1])])
42
+
43
+ /** @type {<T>(o: operator.Fold<T>) => (name: string) => (value: T) => (map: Map<T>) => Map<T>} */
44
+ const setUpdate = o => name => value => setUpdateEntry(o)([name, value])
41
45
 
42
46
  /** @type {(name: string) => <T>(value: T) => (map: Map<T>) => Map<T>} */
43
- const set = name => value => setEntry([name, value])
47
+ const setReplace = name => value => setUpdateEntry(replace)([name, value])
44
48
 
45
49
  /** @type {<T>(map: Map<T>) => list.List<Entry<T>>} */
46
50
  const entries = values
47
51
 
52
+ /** @type {<T>(a: T) => (b: T) => T} */
53
+ const replace = () => b => b
54
+
48
55
  /** @type {<T>(entries: list.List<Entry<T>>) => Map<T>} */
49
- const fromEntries = reduce(setEntry)(undefined)
56
+ const fromEntries = reduce(setUpdateEntry(replace))(undefined)
50
57
 
51
58
  /** @type {(name: string) => <T>(map: Map<T>) => Map<T>} */
52
59
  const remove = name => btreeRemove(keyCmp(name))
@@ -57,7 +64,9 @@ module.exports = {
57
64
  /** @readonly */
58
65
  at,
59
66
  /** @readonly */
60
- set,
67
+ setUpdate,
68
+ /** @readonly */
69
+ setReplace,
61
70
  /** @readonly */
62
71
  entries,
63
72
  /** @readonly */
@@ -1,26 +1,26 @@
1
- const { at, set, empty, entries, remove } = require('./module.f.cjs')
1
+ const { at, setReplace, setUpdate, empty, entries, remove } = require('./module.f.cjs')
2
2
  const seq = require('../list/module.f.cjs')
3
3
 
4
4
  {
5
- let m = set('a')(1)(undefined)
5
+ let m = setReplace('a')(1)(undefined)
6
6
 
7
7
  if (at('a')(m) !== 1) { throw 'error' }
8
8
  if (at('b')(m) !== undefined) { throw 'error' }
9
9
 
10
- m = set('b')(2)(m)
10
+ m = setReplace('b')(2)(m)
11
11
 
12
12
  if (at('a')(m) !== 1) { throw 'error' }
13
13
  if (at('b')(m) !== 2) { throw 'error' }
14
14
  if (at('c')(m) !== undefined) { throw 'error' }
15
15
 
16
- m = set('z')(3)(m)
16
+ m = setReplace('z')(3)(m)
17
17
 
18
18
  if (at('a')(m) !== 1) { throw 'error' }
19
19
  if (at('b')(m) !== 2) { throw 'error' }
20
20
  if (at('z')(m) !== 3) { throw 'error' }
21
21
  if (at('')(m) !== undefined) { throw 'error' }
22
22
 
23
- m = set('')(4)(m)
23
+ m = setReplace('')(4)(m)
24
24
 
25
25
  if (at('a')(m) !== 1) { throw 'error' }
26
26
  if (at('b')(m) !== 2) { throw 'error' }
@@ -28,7 +28,7 @@ const seq = require('../list/module.f.cjs')
28
28
  if (at('')(m) !== 4) { throw 'error' }
29
29
  if (at('Hello world!')(m) !== undefined) { throw 'error' }
30
30
 
31
- m = set('Hello world!')(42)(m)
31
+ m = setReplace('Hello world!')(42)(m)
32
32
 
33
33
  if (at('a')(m) !== 1) { throw 'error' }
34
34
  if (at('b')(m) !== 2) { throw 'error' }
@@ -40,11 +40,14 @@ const seq = require('../list/module.f.cjs')
40
40
  // console.log(Array.from(m.entries()))
41
41
  m = remove('Hello world!')(m)
42
42
  if (at('Hello world!')(m) !== undefined) { throw m }
43
+
44
+ m = setUpdate(a => b => a + b)('a')(43)(m)
45
+ if (at('a')(m) !== 44) { throw 'error' }
43
46
  }
44
47
 
45
48
  {
46
- let m = set('x')(12)(undefined)
47
- m = set('y')(44)(m)
49
+ let m = setReplace('x')(12)(undefined)
50
+ m = setReplace('y')(44)(m)
48
51
  if (at('x')(m) !== 12) { throw 'error' }
49
52
  if (at('y')(m) !== 44) { throw 'error' }
50
53
  if (at('a')(m) !== undefined) { throw 'error' }
@@ -56,7 +59,7 @@ const seq = require('../list/module.f.cjs')
56
59
  /** @type {import('./module.f.cjs').Map<number>} */
57
60
  let m = empty
58
61
  for (let i = 0; i < 100_000; ++i) {
59
- m = set((i * i).toString())(i)(m)
62
+ m = setReplace((i * i).toString())(i)(m)
60
63
  /*
61
64
  console.log()
62
65
  console.log(`# ${i}`)
@@ -1,5 +1,7 @@
1
1
  const { fold } = require('../list/module.f.cjs')
2
2
  const { addition, min: minOp, max: maxOp } = require('../function/operator/module.f.cjs')
3
+ const compare = require('../function/compare/module.f.cjs')
4
+ const { unsafeCmp } = compare
3
5
 
4
6
  const sum = fold(addition)(0)
5
7
 
@@ -7,6 +9,9 @@ const min = fold(minOp)(undefined)
7
9
 
8
10
  const max = fold(maxOp)(undefined)
9
11
 
12
+ /** @type {(a: number) => (b: number) => compare.Sign} */
13
+ const cmp = unsafeCmp
14
+
10
15
  module.exports = {
11
16
  /** @readonly */
12
17
  sum,
@@ -14,4 +19,6 @@ module.exports = {
14
19
  min,
15
20
  /** @readonly */
16
21
  max,
22
+ /** @readonly */
23
+ cmp,
17
24
  }
@@ -1,4 +1,4 @@
1
- const { sum, min, max } = require('./module.f.cjs')
1
+ const { sum, min, max, cmp } = require('./module.f.cjs')
2
2
 
3
3
  {
4
4
  const result = sum([2, 3, 4, 5])
@@ -20,4 +20,9 @@ const { sum, min, max } = require('./module.f.cjs')
20
20
  if (result !== 12) { throw result }
21
21
  }
22
22
 
23
+ {
24
+ const result = cmp(4)(5)
25
+ if (result !== -1) { throw result }
26
+ }
27
+
23
28
  module.exports = {}
@@ -1,6 +1,8 @@
1
1
  const list = require('../list/module.f.cjs')
2
2
  const { fold: listFold, repeat: listRepeat } = list
3
3
  const { compose } = require('../function/module.f.cjs')
4
+ const compare = require('../function/compare/module.f.cjs')
5
+ const { unsafeCmp } = compare
4
6
  const op = require('../function/operator/module.f.cjs')
5
7
  const { join: joinOp, concat: concatOp } = op
6
8
 
@@ -14,6 +16,9 @@ const concat = fold(concatOp)
14
16
  /** @type {(n: string) => (v: number) => string} */
15
17
  const repeat = v => compose(listRepeat(v))(concat)
16
18
 
19
+ /** @type {(a: string) => (b: string) => compare.Sign} */
20
+ const cmp = unsafeCmp
21
+
17
22
  module.exports = {
18
23
  /** @readonly */
19
24
  join,
@@ -21,4 +26,6 @@ module.exports = {
21
26
  concat,
22
27
  /** @readonly */
23
28
  repeat,
29
+ /** @readonly */
30
+ cmp,
24
31
  }
@@ -1,4 +1,4 @@
1
- const { join, concat, repeat } = require('./module.f.cjs')
1
+ const { join, concat, repeat, cmp } = require('./module.f.cjs')
2
2
  const { repeat: repeatList } = require('../list/module.f.cjs')
3
3
 
4
4
  {
@@ -36,4 +36,9 @@ const { repeat: repeatList } = require('../list/module.f.cjs')
36
36
  if (s != 'xxxxx') { throw s }
37
37
  }
38
38
 
39
+ {
40
+ const result = cmp('3')('4')
41
+ if (result !== -1) { throw result }
42
+ }
43
+
39
44
  module.exports = {}
@@ -7,7 +7,7 @@ const {
7
7
  /** @type {(s: StringSet) => list.List<string>} */
8
8
  values,
9
9
  } = btree
10
- const { stringCmp } = require("../function/compare/module.f.cjs")
10
+ const { cmp } = require("../string/module.f.cjs")
11
11
  const list = require('../list/module.f.cjs')
12
12
  const { reduce } = list
13
13
  const { compose } = require('../function/module.f.cjs')
@@ -15,15 +15,15 @@ const { compose } = require('../function/module.f.cjs')
15
15
  /** @typedef {btTypes.Tree<string>} StringSet */
16
16
 
17
17
  /** @type {(value: string) => (set: StringSet) => boolean} */
18
- const contains = value => s => s !== undefined && isFound(find(stringCmp(value))(s).first)
18
+ const contains = value => s => s !== undefined && isFound(find(cmp(value))(s).first)
19
19
 
20
20
  /** @type {(value: string) => (s: StringSet) => StringSet} */
21
- const set = value => btreeSet(stringCmp(value))(value)
21
+ const set = value => btreeSet(cmp(value))(() => value)
22
22
 
23
23
  const fromValues = reduce(set)(undefined)
24
24
 
25
25
  /** @type {(value: string) => (s: StringSet) => StringSet} */
26
- const remove = compose(stringCmp)(btreeRemove)
26
+ const remove = compose(cmp)(btreeRemove)
27
27
 
28
28
  module.exports = {
29
29
  /** @readonly */