functionalscript 0.0.197 → 0.0.198

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/json/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  const seq = require('../sequence')
2
2
  const map = require('../map')
3
3
  const op = require('../sequence/operator')
4
- const option = require('../option')
4
+ const object = require('../object')
5
5
  const array = require('../sequence/array')
6
6
 
7
7
  /**
@@ -27,12 +27,28 @@ const addProperty = value => {
27
27
  return path => f(array.sequence(path))
28
28
  }
29
29
 
30
+ /** @type {(_: string) => seq.Sequence<string>} */
31
+ const stringSerialize = input => seq.list(JSON.stringify(input))
32
+
33
+ /** @type {(_: number) => seq.Sequence<string>} */
34
+ const numberSerialize = input => seq.list(JSON.stringify(input))
35
+
36
+ const nullSerialize = seq.list('null')
37
+
38
+ const trueSerialize = seq.list('true')
39
+
40
+ const falseSerialize = seq.list('false')
41
+
42
+ /** @type {(_: boolean) => seq.Sequence<string>} */
43
+ const boolSerialize = value => value ? trueSerialize : falseSerialize
44
+
30
45
  /** @type {(kv: readonly[string, Json]) => seq.Sequence<string>} */
31
- const property = ([k, v]) => seq.concat(
32
- seq.list(JSON.stringify(k)),
33
- seq.list(':'),
34
- stringSequence(v))
46
+ const propertySerialize = ([k, v]) => seq.concat(
47
+ stringSerialize(k),
48
+ colon,
49
+ serialize(v))
35
50
 
51
+ const colon = seq.list(':')
36
52
  const comma = seq.list(',')
37
53
 
38
54
  /** @type {op.Scan<seq.Sequence<string>, seq.Sequence<string>>} */
@@ -59,48 +75,54 @@ const objectList = list('{')('}')
59
75
  const arrayList = list('[')(']')
60
76
 
61
77
  /** @type {(object: Object) => seq.Sequence<string>} */
62
- const objectStringify = object => {
63
- const _0 = Object.entries(object)
64
- const _1 = array.sequence(_0)
65
- const _2 = map.fromEntries(_1)
66
- const _3 = _2.entries
67
- const _4 = seq.map(property)(_3)
68
- return objectList(_4)
78
+ const objectSerialize = input => {
79
+ const _0 = object.entries(input)
80
+ const _1 = map.fromEntries(_0).entries
81
+ const _2 = seq.map(propertySerialize)(_1)
82
+ return objectList(_2)
69
83
  }
70
84
 
71
85
  /** @type {(input: Array) => seq.Sequence<string>} */
72
- const arrayStringify = input => {
86
+ const arraySerialize = input => {
73
87
  const _0 = array.sequence(input)
74
- const _1 = seq.map(stringSequence)(_0)
88
+ const _1 = seq.map(serialize)(_0)
75
89
  return arrayList(_1)
76
90
  }
77
91
 
78
92
  /** @type {(value: Json) => seq.Sequence<string>} */
79
- const stringSequence = value => {
80
- const x = typeof value
93
+ const serialize = value => {
81
94
  switch (typeof value) {
82
- case 'boolean': { return seq.list(value ? "true" : "false") }
83
- // Note: we shouldn't use JSON.stringify since it has non determenistic behavior.
84
- // In particular: property order could be different.
85
- case 'number': case 'string': { return seq.list(JSON.stringify(value)) }
95
+ case 'boolean': { return boolSerialize(value) }
96
+ case 'number': { return numberSerialize(value) }
97
+ case 'string': { return stringSerialize(value) }
86
98
  default: {
87
- if (value === null) { return seq.list("null") }
88
- if (value instanceof Array) { return arrayStringify(value) }
89
- return objectStringify(value)
99
+ if (value === null) { return nullSerialize }
100
+ if (value instanceof Array) { return arraySerialize(value) }
101
+ return objectSerialize(value)
90
102
  }
91
103
  }
92
104
  }
93
105
 
94
106
  /**
95
- * A deterministic version of `JSON.stringify`
107
+ * A version of `JSON.stringify` with an alphabeticly ordered `keys`.
108
+ *
109
+ * The standard `JSON.stringify` rules determines by
110
+ * https://262.ecma-international.org/6.0/#sec-ordinary-object-internal-methods-and-internal-slots-ownpropertykeys
96
111
  *
97
112
  * @type {(value: Json) => string}
98
113
  */
99
- const stringify = value => seq.join('')(stringSequence(value))
114
+ const stringify = value => seq.join('')(serialize(value))
115
+
116
+ /** @type {(value: string) => Json} */
117
+ const parse = value => JSON.parse(value)
100
118
 
101
119
  module.exports = {
102
120
  /** @readonly */
103
121
  addProperty,
104
122
  /** @readonly */
105
123
  stringify,
124
+ /** @readonly */
125
+ serialize,
126
+ /** @readonly */
127
+ parse,
106
128
  }
@@ -0,0 +1,17 @@
1
+ const array = require('../sequence/array')
2
+ const seq = require('../sequence')
3
+
4
+ /**
5
+ * @template T
6
+ * @typedef {{
7
+ * readonly [k in string]: T
8
+ * }} Object_
9
+ */
10
+
11
+ /** @type {<T>(object: Object_<T>) => seq.Sequence<readonly[string, T]>} */
12
+ const entries = object => array.sequence(Object.entries(object))
13
+
14
+ module.exports = {
15
+ /** @readonly */
16
+ entries,
17
+ }
package/package.json CHANGED
@@ -1,25 +1,25 @@
1
1
  {
2
- "name": "functionalscript",
3
- "version": "0.0.197",
4
- "description": "FunctionalScript is a functional subset of JavaScript",
5
- "main": "index.js",
6
- "scripts": {
7
- "tsc": "tsc",
8
- "test": "tsc && npm run test-only",
9
- "test-only": "node --trace-uncaught ./test"
10
- },
11
- "repository": {
12
- "type": "git",
13
- "url": "git+https://github.com/functionalscript/functionalscript.git"
14
- },
15
- "author": "Natfoam",
16
- "license": "Apache-2.0",
17
- "bugs": {
18
- "url": "https://github.com/functionalscript/functionalscript/issues"
19
- },
20
- "homepage": "https://github.com/functionalscript/functionalscript#readme",
21
- "devDependencies": {
22
- "@types/node": "^16.11.9",
23
- "typescript": "^4.5.2"
24
- }
2
+ "name": "functionalscript",
3
+ "version": "0.0.198",
4
+ "description": "FunctionalScript is a functional subset of JavaScript",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "tsc": "tsc",
8
+ "test": "tsc && npm run test-only",
9
+ "test-only": "node --trace-uncaught ./test"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+https://github.com/functionalscript/functionalscript.git"
14
+ },
15
+ "author": "Natfoam",
16
+ "license": "Apache-2.0",
17
+ "bugs": {
18
+ "url": "https://github.com/functionalscript/functionalscript/issues"
19
+ },
20
+ "homepage": "https://github.com/functionalscript/functionalscript#readme",
21
+ "devDependencies": {
22
+ "@types/node": "^16.11.9",
23
+ "typescript": "^4.5.2"
24
+ }
25
25
  }
@@ -107,6 +107,9 @@ const sequence = a => {
107
107
  return seq(0)
108
108
  }
109
109
 
110
+ /** @type {<T>(list: seq.Sequence<T>) => readonly T[]} */
111
+ const fromSequence = input => Array.from(seq.iterable(input))
112
+
110
113
  module.exports = {
111
114
  /** @readonly */
112
115
  at,
@@ -124,4 +127,6 @@ module.exports = {
124
127
  splitLast,
125
128
  /** @readonly */
126
129
  sequence,
130
+ /** @readonly */
131
+ fromSequence,
127
132
  }
package/sequence/index.js CHANGED
@@ -77,19 +77,23 @@ const first = input => {
77
77
  * @typedef {(list: Sequence<T>) => R} SequenceReduce
78
78
  */
79
79
 
80
- /** @type {<T>(...array: readonly T[]) => Sequence<T>} */
80
+ /**
81
+ * Note: the operation is not lazy. It traverse the given array and creates a linked list.
82
+ *
83
+ * @type {<T>(...array: readonly T[]) => Sequence<T>}
84
+ */
81
85
  const list = (...array) => {
82
86
  /** @typedef {typeof array extends readonly(infer T)[] ? T : never} T */
83
87
  let i = array.length
84
88
  /** @type {Sequence<T>} */
85
- let tail = empty
86
- while (true) {
87
- if (i === 0) { return tail }
89
+ let result = empty
90
+ while (i !== 0) {
88
91
  i = i - 1
89
92
  /** @type {FirstAndTail<T>} */
90
- const result = [array[i], tail]
91
- tail = () => result
93
+ const listResult = [array[i], result]
94
+ result = () => listResult
92
95
  }
96
+ return result
93
97
  }
94
98
 
95
99
  /** @type {<T>(...array: readonly Sequence<T>[]) => Sequence<T>} */
@@ -97,12 +101,12 @@ const concat = (...array) => {
97
101
  let i = array.length
98
102
  if (i == 0) { return empty }
99
103
  i = i - 1
100
- let tail = array[i]
101
- while (true) {
102
- if (i === 0) { return tail }
104
+ let result = array[i]
105
+ while (i !== 0) {
103
106
  i = i - 1
104
- tail = [array[i], tail]
107
+ result = [array[i], result]
105
108
  }
109
+ return result
106
110
  }
107
111
 
108
112
  /** @type {(_: number) => Sequence<number>} */
@@ -261,9 +265,6 @@ const zip = a => b => () => {
261
265
  return [[resultA[0], resultB[0]], zip(resultA[1])(resultB[1])]
262
266
  }
263
267
 
264
- /** @type {<T>(list: Sequence<T>) => readonly T[]} */
265
- const toArray = input => Array.from(iterable(input))
266
-
267
268
  module.exports = {
268
269
  /** @readonly */
269
270
  next,
@@ -280,8 +281,6 @@ module.exports = {
280
281
  /** @readonly */
281
282
  first,
282
283
  /** @readonly */
283
- toArray,
284
- /** @readonly */
285
284
  iterable,
286
285
  /** @readonly */
287
286
  asyncIterable,
package/sequence/test.js CHANGED
@@ -27,15 +27,18 @@ const print = input => {
27
27
  if (seq.some(x => x > 50)(big) !== true) { throw 'x' }
28
28
  if (seq.first(seq.drop(16)(big)) !== 42) { throw 'drop'}
29
29
  {
30
- const a = seq.toArray(seq.generate(1_000_000))
31
- let x = seq.concat(array.sequence(a), big)
30
+ const a = seq.map(seq.generate)(seq.generate(100_000))
31
+ const ar = array.fromSequence(a)
32
+ // This operation use a lot of stack because `...`
33
+ // puts array items on a stack.
34
+ // Use `array.sequence` instead
35
+ const x = seq.concat(...ar)
32
36
  const r = seq.next(x)
33
37
  // print(x)
34
38
  }
35
39
  {
36
- const a = seq.map(seq.generate)(seq.generate(100_000))
37
- const array = seq.toArray(a)
38
- const x = seq.concat(...array)
40
+ const a = array.fromSequence(seq.generate(1_000_000))
41
+ let x = seq.concat(array.sequence(a), big)
39
42
  const r = seq.next(x)
40
43
  // print(x)
41
44
  }
package/version.js CHANGED
@@ -1,5 +1,6 @@
1
1
  const cp = require('child_process')
2
2
  const fs = require('fs')
3
+ const json = require('./json')
3
4
 
4
5
  const b =cp.execSync('git log --oneline')
5
6
 
@@ -9,8 +10,8 @@ const v = `0.0.${r}`
9
10
 
10
11
  console.log(`version: ${v}`)
11
12
 
12
- const package_json = JSON.parse(fs.readFileSync('package.json').toString())
13
+ let package_json = json.parse(fs.readFileSync('package.json').toString())
13
14
 
14
- package_json.version = v
15
+ package_json = json.addProperty(v)(['version'])(package_json)
15
16
 
16
- fs.writeFileSync('package.json', JSON.stringify(package_json, null, 2))
17
+ fs.writeFileSync('package.json', JSON.stringify(package_json, null, 3))