functionalscript 0.0.148 → 0.0.152

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/lib/index.js CHANGED
@@ -28,48 +28,18 @@
28
28
  /** @type {(_: string) => never} */
29
29
  const panic = message => { throw message }
30
30
 
31
- /**
32
- * @template T
33
- * @typedef {{
34
- * readonly get: (_: string) => T|undefined
35
- * readonly set: (_: string) => (_: T) => Dictionary<T>
36
- * readonly entries: () => Iterable<[string, T]>
37
- * }} Dictionary
38
- */
39
-
40
- /**
41
- * @template T
42
- * @typedef {{
43
- * readonly [_ in string]: T
44
- * }} InternalMap
45
- */
46
-
47
- const dictionary = {
48
- get: () => undefined,
49
- /** @type {(_: string) => <T>(_: T) => Dictionary<T>} */
50
- set: key => value => createDictionary({[key]: value}),
51
- entries: () => []
52
- }
53
-
54
- /** @type {<T>(_: InternalMap<T>) => Dictionary<T>} */
55
- const createDictionary = internal => ({
56
- get: key => internal[key],
57
- set: key => value => createDictionary({ ...internal, [key]: value }),
58
- entries: () => Object.entries(internal),
59
- })
60
-
61
31
  /** @type {<A>(_: A) => <B>(_: B) => [A, B]} */
62
32
  const tuple2 = a => b => [a, b]
63
33
 
64
- /** @type {<T>(_: (_: string) => T) => (_: Dictionary<T>) => (_: string) => [T, Dictionary<T>]} */
65
- const cache = f => d => k => {
66
- const set = () => {
67
- const r = f(k)
68
- return tuple2(r)(d.set(k)(r))
69
- }
70
- const result = d.get(k)
71
- return result !== undefined ? [result, d] : set()
72
- }
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
+ // }
73
43
 
74
44
  /**
75
45
  * @template T
@@ -133,8 +103,6 @@ module.exports = {
133
103
  /** @type {(_: string) => (_: boolean) => void} */
134
104
  panic_if: message => condition => condition ? panic(message) : undefined,
135
105
 
136
- dictionary,
137
-
138
106
  /** @type {<T>(_: Continuation<T>) => T} */
139
107
  trampoline: continuation => {
140
108
  while (true) {
@@ -165,8 +133,6 @@ module.exports = {
165
133
 
166
134
  optionMap,
167
135
 
168
- cache,
169
-
170
136
  /** @type {<T>(_: T[]) => T[]|undefined} */
171
137
  tail: a => a.length === 0 ? undefined : uncheckTail(a),
172
138
 
package/lib/map/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  const { optionMap } = require("..")
2
- const { getVisitor, setVisitor, values } = require("../tree")
2
+ const { getVisitor, setVisitor, values, struct } = require("../tree")
3
3
 
4
4
  /** @typedef {import("../tree/cmp").Sign} Sign */
5
5
 
@@ -29,6 +29,7 @@ const { getVisitor, setVisitor, values } = require("../tree")
29
29
  * readonly get: (name: string) => T|undefined
30
30
  * readonly set: (name: string) => (value: T) => Map<T>
31
31
  * readonly entries: () => Iterable<Entry<T>>
32
+ * readonly struct: () => Iterable<string>
32
33
  * }} Map
33
34
  */
34
35
 
@@ -38,13 +39,14 @@ const cmp = a => ([b]) => a < b ? -1 : a === b ? 0 : 1
38
39
  /** @type {<T>(node: TNode<Entry<T>>) => Map<T>} */
39
40
  const create = node => ({
40
41
  get: name => optionMap(([,value]) => value)(getVisitor(cmp(name))(node)),
41
- set: name => value => {
42
+ set: name => value => {
42
43
  const result = setVisitor(cmp(name))(() => [name, value])(node)
43
44
  if ('replace' in result) { return create(result.replace) }
44
45
  if ('overflow' in result) { return create(result.overflow) }
45
46
  throw ''
46
47
  },
47
- entries: () => values(node)
48
+ entries: () => values(node),
49
+ struct: () => struct(node),
48
50
  })
49
51
 
50
52
  /**
@@ -52,12 +54,14 @@ const create = node => ({
52
54
  * readonly get: (name: string) => undefined
53
55
  * readonly set: (name: string) => <T>(value: T) => Map<T>
54
56
  * readonly entries: () => []
57
+ * readonly struct: () => []
55
58
  * }}
56
59
  */
57
60
  const map = {
58
61
  get: () => undefined,
59
62
  set: name => value => create([[name, value]]),
60
63
  entries: () => [],
64
+ struct: () => [],
61
65
  }
62
66
 
63
67
  module.exports = {
package/lib/map/test.js CHANGED
@@ -40,26 +40,26 @@ const lib = require('..')
40
40
  // console.log(Array.from(m.entries()))
41
41
  }
42
42
 
43
+ {
44
+ const m = map.set('x')(12).set('y')(44)
45
+ lib.panic_if('map.get(\'x\')')(m.get('x') !== 12)
46
+ lib.panic_if('map.get(\'y\')')(m.get('y') !== 44)
47
+ lib.panic_if('map.get(\'a\')')(m.get('a') !== undefined)
48
+ const entries = Array.from(m.entries())
49
+ lib.panic_if('map.entries()')(entries.length !== 2)
50
+ }
51
+
52
+
43
53
  {
44
54
  /** @type {import('.').Map<number>} */
45
55
  let m = map
46
- for (let i = 0; i < 1_000_000; ++i) {
47
- m = m.set(i.toString())(i * i)
48
- if (i % 100_000 === 0) { console.log(i) }
49
- }
50
- for (const e of m.entries()) {
51
- // console.log(e)
56
+ for (let i = 0; i < 10; ++i) {
57
+ m = m.set((i*i).toString())(i)
58
+ console.log()
59
+ console.log(`# ${i}`)
60
+ console.log()
61
+ for (const e of m.struct()) {
62
+ console.log(e)
63
+ }
52
64
  }
53
65
  }
54
- // console.log(':')
55
- {
56
- /** @type {import('..').Dictionary<number>} */
57
- let m = lib.dictionary
58
- for (let i = 0; i < 100_000; ++i) {
59
- m = m.set(i.toString())(i * i)
60
- if (i % 10_000 === 0) { console.log(i) }
61
- }
62
- for (const e of m.entries()) {
63
- // console.log(e)
64
- }
65
- }
package/lib/test.js CHANGED
@@ -3,11 +3,3 @@ const lib = require('./index')
3
3
  require('./iterable/test')
4
4
  require('./map/test')
5
5
 
6
- {
7
- const map = lib.dictionary.set('x')(12).set('y')(44)
8
- lib.panic_if('map.get(\'x\')')(map.get('x') !== 12)
9
- lib.panic_if('map.get(\'y\')')(map.get('y') !== 44)
10
- lib.panic_if('map.get(\'a\')')(map.get('a') !== undefined)
11
- const entries = Array.from(map.entries())
12
- lib.panic_if('map.entries()')(entries.length !== 2)
13
- }
package/lib/tree/index.js CHANGED
@@ -319,7 +319,49 @@ const values = node => ({
319
319
  }
320
320
  })
321
321
 
322
+ /**
323
+ * @typedef {{}}
324
+ */
325
+
326
+ /** @type {(offset: string) => <T>(_: TNode<T>) => Iterable<string>} */
327
+ const struct = offset => node => ({
328
+ *[Symbol.iterator]() {
329
+ const next = `${offset} `
330
+ switch (node.length) {
331
+ case 1: {
332
+ yield `${offset}- [1]:`
333
+ yield `${next}- ${node[0]}`
334
+ return
335
+ }
336
+ case 2: {
337
+ yield `${offset}- [2]:`
338
+ yield `${next}- ${node[0]}`
339
+ yield `${next}- ${node[1]}`
340
+ return
341
+ }
342
+ case 3: {
343
+ yield `${offset}- [3]:`
344
+ yield* struct(next)(node[0])
345
+ yield `${next}- ${node[1]}`
346
+ yield* struct(next)(node[2])
347
+ return
348
+ }
349
+ default: {
350
+ yield `${offset}- [5]:`
351
+ yield* struct(next)(node[0])
352
+ yield `${next}- ${node[1]}`
353
+ yield* struct(next)(node[2])
354
+ yield `${next}- ${node[3]}`
355
+ yield* struct(next)(node[4])
356
+ return
357
+ }
358
+ }
359
+ }
360
+ })
361
+
322
362
  module.exports = {
363
+ /** @readonly */
364
+ struct: struct(''),
323
365
  /** @readonly */
324
366
  values,
325
367
  /**
@@ -327,7 +369,7 @@ module.exports = {
327
369
  * @type {<T>(cmp: Cmp<T>) => (node: TNode<T>) => T|undefined}
328
370
  */
329
371
  getVisitor: cmp => node => {
330
- const result = visit(getVisitor)(cmp)(() => { throw ''})(node)
372
+ const result = visit(getVisitor)(cmp)(() => { throw '' })(node)
331
373
  if ('done' in result && result.done) { return result.value }
332
374
  return undefined
333
375
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "functionalscript",
3
- "version": "0.0.148",
3
+ "version": "0.0.152",
4
4
  "description": "FunctionalScript is a functional subset of JavaScript",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -19,7 +19,7 @@
19
19
  },
20
20
  "homepage": "https://github.com/functionalscript/functionalscript#readme",
21
21
  "devDependencies": {
22
- "@types/node": "^16.11.7",
23
- "typescript": "^4.4.4"
22
+ "@types/node": "^16.11.9",
23
+ "typescript": "^4.5.2"
24
24
  }
25
25
  }