functionalscript 0.3.9 → 0.3.11
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/README.md +1 -0
- package/crypto/prime_field/module.f.d.ts +1 -4
- package/crypto/secp/module.f.d.ts +2 -2
- package/fsc/module.f.js +4 -6
- package/fsm/module.f.d.ts +9 -9
- package/fsm/module.f.js +12 -21
- package/html/module.f.js +1 -2
- package/nodejs/version/module.f.d.ts +1 -1
- package/package.json +1 -1
- package/types/bigint/module.f.d.ts +2 -2
- package/types/bigint/module.f.js +2 -2
- package/types/btree/find/module.f.d.ts +2 -2
- package/types/btree/remove/module.f.d.ts +4 -4
- package/types/btree/remove/module.f.js +9 -11
- package/types/btree/remove/test.f.js +2 -2
- package/types/btree/set/module.f.d.ts +3 -3
- package/types/byte_set/test.f.js +31 -31
- package/types/function/module.f.d.ts +1 -1
- package/types/range_map/test.f.js +23 -23
- package/types/sorted_list/module.f.js +3 -3
- package/types/sorted_set/module.f.d.ts +28 -0
- package/types/sorted_set/test.f.d.ts +1 -0
- package/types/sorted_set/test.f.js +19 -0
- package/types/string/module.f.d.ts +17 -0
- package/types/string/module.f.js +17 -0
- package/types/string/test.f.d.ts +1 -0
- package/types/string/test.f.js +15 -0
- package/types/string_set/module.f.d.ts +21 -0
- package/types/string_set/module.f.js +21 -0
- package/types/string_set/test.f.d.ts +1 -0
- package/types/string_set/test.f.js +18 -1
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ FunctionalScript is a safe, purely functional programming language and a strict
|
|
|
15
15
|
[A working draft of the FunctionalScript specification](./issues/lang/README.md).
|
|
16
16
|
|
|
17
17
|
Learn more about
|
|
18
|
+
|
|
18
19
|
- [Purely Functional Programming in JavaScript](https://blog.bitsrc.io/purely-functional-programming-in-javascript-91114b1b2dff?sk=5f7132e56902f38fcf4c6164bfa681ed),
|
|
19
20
|
- [FunctionalScript and I/O](https://medium.com/@sergeyshandar/functionalscript-5cf817345376?sk=30b32189a81d1a2dad16c2244f32328d).
|
|
20
21
|
|
|
@@ -1,6 +1,4 @@
|
|
|
1
|
-
import type
|
|
2
|
-
type Reduce = Operator.Reduce<bigint>;
|
|
3
|
-
type Unary = Operator.Unary<bigint, bigint>;
|
|
1
|
+
import type { Unary, Reduce } from '../../types/bigint/module.f.ts';
|
|
4
2
|
/**
|
|
5
3
|
* A type representing a prime field and its associated operations.
|
|
6
4
|
*/
|
|
@@ -40,4 +38,3 @@ export declare const prime_field: (p: bigint) => PrimeField;
|
|
|
40
38
|
* ```
|
|
41
39
|
*/
|
|
42
40
|
export declare const sqrt: ({ p, mul, pow }: PrimeField) => (a: bigint) => bigint | null;
|
|
43
|
-
export {};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type { Reduce } from '../../types/function/operator/module.f.ts';
|
|
2
2
|
import { type PrimeField } from '../prime_field/module.f.ts';
|
|
3
3
|
/**
|
|
4
4
|
* A 2D point represented as a pair of `bigint` values `[x, y]`.
|
|
@@ -27,7 +27,7 @@ type Curve = {
|
|
|
27
27
|
readonly y2: (x: bigint) => bigint;
|
|
28
28
|
readonly y: (x: bigint) => bigint | null;
|
|
29
29
|
readonly neg: (a: Point) => Point;
|
|
30
|
-
readonly add:
|
|
30
|
+
readonly add: Reduce<Point>;
|
|
31
31
|
readonly mul: (a: Point) => (n: bigint) => Point;
|
|
32
32
|
};
|
|
33
33
|
/**
|
package/fsc/module.f.js
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { strictEqual } from "../types/function/operator/module.f.js";
|
|
2
2
|
import { merge as rangeMapMerge, fromRange, get, } from "../types/range_map/module.f.js";
|
|
3
|
-
import
|
|
4
|
-
const { reduce: listReduce } = list;
|
|
3
|
+
import { reduce as listReduce, toArray, map } from "../types/list/module.f.js";
|
|
5
4
|
import { range as asciiRange } from "../text/ascii/module.f.js";
|
|
6
5
|
import { fn } from "../types/function/module.f.js";
|
|
7
6
|
import { one } from "../types/range/module.f.js";
|
|
8
|
-
const { toArray, map } = list;
|
|
9
7
|
const fromCharCode = String.fromCharCode;
|
|
10
8
|
const unexpectedSymbol = codePoint => [[`unexpected symbol ${codePoint}`], unexpectedSymbol];
|
|
11
9
|
const def = () => unexpectedSymbol;
|
|
12
|
-
const union = a => b => {
|
|
10
|
+
const union = (a) => (b) => {
|
|
13
11
|
if (a === def || a === b) {
|
|
14
12
|
return b;
|
|
15
13
|
}
|
|
@@ -22,7 +20,7 @@ const empty = [];
|
|
|
22
20
|
const reduce = (a) => {
|
|
23
21
|
const merge = rangeMapMerge({
|
|
24
22
|
union,
|
|
25
|
-
equal:
|
|
23
|
+
equal: strictEqual,
|
|
26
24
|
});
|
|
27
25
|
return toArray(listReduce(merge)(empty)(a));
|
|
28
26
|
};
|
package/fsm/module.f.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
type Rule = readonly [string,
|
|
5
|
-
export type Grammar =
|
|
1
|
+
import { type List } from '../types/list/module.f.ts';
|
|
2
|
+
import { type ByteSet } from '../types/byte_set/module.f.ts';
|
|
3
|
+
import { type RangeMapArray } from '../types/range_map/module.f.ts';
|
|
4
|
+
type Rule = readonly [string, ByteSet, string];
|
|
5
|
+
export type Grammar = List<Rule>;
|
|
6
6
|
type Dfa = {
|
|
7
|
-
readonly [state in string]:
|
|
7
|
+
readonly [state in string]: RangeMapArray<string>;
|
|
8
8
|
};
|
|
9
|
-
export declare const toRange: (s: string) =>
|
|
10
|
-
export declare const toUnion: (s: string) =>
|
|
9
|
+
export declare const toRange: (s: string) => ByteSet;
|
|
10
|
+
export declare const toUnion: (s: string) => ByteSet;
|
|
11
11
|
export declare const dfa: (grammar: Grammar) => Dfa;
|
|
12
|
-
export declare const run: (dfa: Dfa) => (input:
|
|
12
|
+
export declare const run: (dfa: Dfa) => (input: List<number>) => List<string>;
|
|
13
13
|
export {};
|
package/fsm/module.f.js
CHANGED
|
@@ -1,25 +1,16 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
import
|
|
10
|
-
const { unsafeCmp } = cmp;
|
|
11
|
-
import * as operator from "../types/function/operator/module.f.js";
|
|
12
|
-
const { strictEqual } = operator;
|
|
13
|
-
import * as j from "../json/module.f.js";
|
|
14
|
-
const { stringify } = j;
|
|
15
|
-
import * as f from "../types/function/module.f.js";
|
|
16
|
-
const { identity } = f;
|
|
17
|
-
import * as utf16 from "../text/utf16/module.f.js";
|
|
18
|
-
const { stringToList } = utf16;
|
|
1
|
+
import { equal, isEmpty, fold, toArray, scan, foldScan, empty as emptyList } from "../types/list/module.f.js";
|
|
2
|
+
import { toRangeMap, union as byteSetUnion, one, empty, range } from "../types/byte_set/module.f.js";
|
|
3
|
+
import { intersect, union as sortedSetUnion } from "../types/sorted_set/module.f.js";
|
|
4
|
+
import { merge, get as rangeMapGet } from "../types/range_map/module.f.js";
|
|
5
|
+
import { unsafeCmp } from "../types/function/compare/module.f.js";
|
|
6
|
+
import { strictEqual } from "../types/function/operator/module.f.js";
|
|
7
|
+
import { stringify } from "../json/module.f.js";
|
|
8
|
+
import { identity } from "../types/function/module.f.js";
|
|
9
|
+
import { stringToList } from "../text/utf16/module.f.js";
|
|
19
10
|
const stringifyIdentity = stringify(identity);
|
|
20
11
|
export const toRange = s => {
|
|
21
12
|
const [b, e] = toArray(stringToList(s));
|
|
22
|
-
return
|
|
13
|
+
return range([b, e]);
|
|
23
14
|
};
|
|
24
15
|
const toUnionOp = i => bs => byteSetUnion(bs)(one(i));
|
|
25
16
|
export const toUnion = s => {
|
|
@@ -54,6 +45,6 @@ const emptyStateStringify = stringifyIdentity(emptyState);
|
|
|
54
45
|
const initialState = [''];
|
|
55
46
|
const initialStateStringify = stringifyIdentity(initialState);
|
|
56
47
|
export const dfa = grammar => addEntry(grammar)(initialState)({});
|
|
57
|
-
const get =
|
|
48
|
+
const get = rangeMapGet(emptyStateStringify);
|
|
58
49
|
const runOp = dfa => input => s => get(input)(dfa[s]);
|
|
59
|
-
export const run = dfa => input => foldScan(runOp(dfa))(initialStateStringify)(input);
|
|
50
|
+
export const run = (dfa) => (input) => foldScan(runOp(dfa))(initialStateStringify)(input);
|
package/html/module.f.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
import { map, flatMap, flat, concat as listConcat } from "../types/list/module.f.js";
|
|
2
2
|
import { concat as stringConcat } from "../types/string/module.f.js";
|
|
3
3
|
import { compose } from "../types/function/module.f.js";
|
|
4
|
-
import
|
|
5
|
-
const { stringToList } = utf16;
|
|
4
|
+
import { stringToList } from "../text/utf16/module.f.js";
|
|
6
5
|
const { fromCharCode } = String;
|
|
7
6
|
const { entries } = Object;
|
|
8
7
|
/**
|
package/package.json
CHANGED
package/types/bigint/module.f.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Leaf1, Leaf2, Branch3, Branch5, TNode } from '../types/module.f.ts';
|
|
2
|
-
import type
|
|
2
|
+
import type { List } from '../../list/module.f.ts';
|
|
3
3
|
import { type Compare } from '../../function/compare/module.f.ts';
|
|
4
4
|
import type { Index3, Index5 } from "../../array/module.f.ts";
|
|
5
5
|
type FirstLeaf1<T> = readonly [Index3, Leaf1<T>];
|
|
@@ -10,7 +10,7 @@ type First<T> = FirstLeaf1<T> | FirstBranch3<T> | FirstLeaf2<T> | FirstBranch5<T
|
|
|
10
10
|
type PathItem3<T> = readonly [0 | 2, Branch3<T>];
|
|
11
11
|
type PathItem5<T> = readonly [0 | 2 | 4, Branch5<T>];
|
|
12
12
|
export type PathItem<T> = PathItem3<T> | PathItem5<T>;
|
|
13
|
-
export type Path<T> = List
|
|
13
|
+
export type Path<T> = List<PathItem<T>>;
|
|
14
14
|
export type Result<T> = {
|
|
15
15
|
readonly first: First<T>;
|
|
16
16
|
readonly tail: Path<T>;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
export declare const nodeRemove: <T>(c:
|
|
4
|
-
export declare const remove: <T>(c:
|
|
1
|
+
import type { TNode, Tree } from '../types/module.f.ts';
|
|
2
|
+
import type { Compare } from '../../function/compare/module.f.ts';
|
|
3
|
+
export declare const nodeRemove: <T>(c: Compare<T>) => (node: TNode<T>) => Tree<T>;
|
|
4
|
+
export declare const remove: <T>(c: Compare<T>) => (tree: Tree<T>) => Tree<T>;
|
|
@@ -1,9 +1,7 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { map } = n;
|
|
6
|
-
const path = tail => n => {
|
|
1
|
+
import { find } from "../find/module.f.js";
|
|
2
|
+
import { fold, concat, next } from "../../list/module.f.js";
|
|
3
|
+
import { map } from "../../nullable/module.f.js";
|
|
4
|
+
const path = (tail) => (n) => {
|
|
7
5
|
switch (n.length) {
|
|
8
6
|
case 1: {
|
|
9
7
|
return [n[0], { first: null, tail }];
|
|
@@ -19,7 +17,7 @@ const path = tail => n => {
|
|
|
19
17
|
}
|
|
20
18
|
}
|
|
21
19
|
};
|
|
22
|
-
const reduceValue0 = a => n => {
|
|
20
|
+
const reduceValue0 = (a) => (n) => {
|
|
23
21
|
const [, v1, n2] = n;
|
|
24
22
|
if (a.length === 1) {
|
|
25
23
|
switch (n2.length) {
|
|
@@ -38,7 +36,7 @@ const reduceValue0 = a => n => {
|
|
|
38
36
|
return [a, v1, n2];
|
|
39
37
|
}
|
|
40
38
|
};
|
|
41
|
-
const reduceValue2 = a => n => {
|
|
39
|
+
const reduceValue2 = (a) => (n) => {
|
|
42
40
|
const [n0, v1,] = n;
|
|
43
41
|
if (a.length === 1) {
|
|
44
42
|
switch (n0.length) {
|
|
@@ -57,7 +55,7 @@ const reduceValue2 = a => n => {
|
|
|
57
55
|
return [n0, v1, a];
|
|
58
56
|
}
|
|
59
57
|
};
|
|
60
|
-
const initValue0 = a => n => {
|
|
58
|
+
const initValue0 = (a) => (n) => {
|
|
61
59
|
const [, v1, n2] = n;
|
|
62
60
|
if (a === null) {
|
|
63
61
|
switch (n2.length) {
|
|
@@ -76,7 +74,7 @@ const initValue0 = a => n => {
|
|
|
76
74
|
return [a, v1, n2];
|
|
77
75
|
}
|
|
78
76
|
};
|
|
79
|
-
const initValue1 = a => n => {
|
|
77
|
+
const initValue1 = (a) => (n) => {
|
|
80
78
|
const [n0, v1] = n;
|
|
81
79
|
if (a === null) {
|
|
82
80
|
switch (n0.length) {
|
|
@@ -117,7 +115,7 @@ const reduce = fold(reduceX([reduceValue0, reduceValue2]));
|
|
|
117
115
|
const initReduce = reduceX([initValue0, initValue1]);
|
|
118
116
|
export const nodeRemove = (c) => (node) => {
|
|
119
117
|
const f = () => {
|
|
120
|
-
const { first, tail } = find
|
|
118
|
+
const { first, tail } = find(c)(node);
|
|
121
119
|
const branch = n => f => {
|
|
122
120
|
const [v, p] = path(null)(n);
|
|
123
121
|
return { first: p.first, tail: concat(p.tail)({ first: f(v), tail }) };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { nodeRemove } from "./module.f.js";
|
|
2
2
|
import * as s from "../set/module.f.js";
|
|
3
3
|
import { cmp } from "../../string/module.f.js";
|
|
4
4
|
import * as json from "../../../json/module.f.js";
|
|
5
5
|
import { sort } from "../../object/module.f.js";
|
|
6
6
|
const set = (node) => (value) => s.set(cmp(value))(() => value)(node);
|
|
7
|
-
const remove = (node) => (value) =>
|
|
7
|
+
const remove = (node) => (value) => nodeRemove(cmp(value))(node);
|
|
8
8
|
const jsonStr = json.stringify(sort);
|
|
9
9
|
const test = () => {
|
|
10
10
|
let _map = ['1'];
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type
|
|
3
|
-
export declare const set: <T>(c:
|
|
1
|
+
import type { TNode, Tree } from '../types/module.f.ts';
|
|
2
|
+
import type { Compare } from '../../function/compare/module.f.ts';
|
|
3
|
+
export declare const set: <T>(c: Compare<T>) => (f: (value: T | null) => T) => (tree: Tree<T>) => TNode<T>;
|
package/types/byte_set/test.f.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { has, empty, set, setRange, unset, universe, complement, toRangeMap } from "./module.f.js";
|
|
2
2
|
import { every, countdown, map, toArray } from "../list/module.f.js";
|
|
3
3
|
import * as json from "../../json/module.f.js";
|
|
4
4
|
import { sort } from "../object/module.f.js";
|
|
@@ -6,112 +6,112 @@ const stringify = json.stringify(sort);
|
|
|
6
6
|
export default {
|
|
7
7
|
has: [
|
|
8
8
|
() => {
|
|
9
|
-
if (
|
|
10
|
-
throw
|
|
9
|
+
if (has(0)(empty)) {
|
|
10
|
+
throw empty;
|
|
11
11
|
}
|
|
12
|
-
if (
|
|
13
|
-
throw
|
|
12
|
+
if (has(1)(empty)) {
|
|
13
|
+
throw empty;
|
|
14
14
|
}
|
|
15
|
-
if (
|
|
16
|
-
throw
|
|
15
|
+
if (has(33)(empty)) {
|
|
16
|
+
throw empty;
|
|
17
17
|
}
|
|
18
18
|
},
|
|
19
19
|
() => {
|
|
20
|
-
const s =
|
|
20
|
+
const s = set(0)(empty);
|
|
21
21
|
if (s !== 1n) {
|
|
22
22
|
throw s;
|
|
23
23
|
}
|
|
24
|
-
if (!
|
|
24
|
+
if (!has(0)(s)) {
|
|
25
25
|
throw s;
|
|
26
26
|
}
|
|
27
|
-
if (
|
|
27
|
+
if (has(1)(s)) {
|
|
28
28
|
throw s;
|
|
29
29
|
}
|
|
30
|
-
if (
|
|
30
|
+
if (has(33)(s)) {
|
|
31
31
|
throw s;
|
|
32
32
|
}
|
|
33
33
|
},
|
|
34
34
|
() => {
|
|
35
|
-
const s =
|
|
35
|
+
const s = set(33)(empty);
|
|
36
36
|
if (s !== 8589934592n) {
|
|
37
37
|
throw s;
|
|
38
38
|
}
|
|
39
|
-
if (
|
|
39
|
+
if (has(0)(s)) {
|
|
40
40
|
throw s;
|
|
41
41
|
}
|
|
42
|
-
if (
|
|
42
|
+
if (has(1)(s)) {
|
|
43
43
|
throw s;
|
|
44
44
|
}
|
|
45
|
-
if (!
|
|
45
|
+
if (!has(33)(s)) {
|
|
46
46
|
throw s;
|
|
47
47
|
}
|
|
48
48
|
}
|
|
49
49
|
],
|
|
50
50
|
setRange: () => {
|
|
51
|
-
const result =
|
|
51
|
+
const result = setRange([2, 5])(empty);
|
|
52
52
|
if (result !== 60n) {
|
|
53
53
|
throw result;
|
|
54
54
|
}
|
|
55
55
|
},
|
|
56
56
|
unset: [
|
|
57
57
|
() => {
|
|
58
|
-
const a =
|
|
59
|
-
const result =
|
|
58
|
+
const a = set(0)(empty);
|
|
59
|
+
const result = unset(0)(a);
|
|
60
60
|
if (result !== 0n) {
|
|
61
61
|
throw result;
|
|
62
62
|
}
|
|
63
63
|
},
|
|
64
64
|
() => {
|
|
65
|
-
const a =
|
|
66
|
-
const result =
|
|
65
|
+
const a = set(255)(empty);
|
|
66
|
+
const result = unset(255)(a);
|
|
67
67
|
if (result !== 0n) {
|
|
68
68
|
throw result;
|
|
69
69
|
}
|
|
70
70
|
}
|
|
71
71
|
],
|
|
72
72
|
universe: () => {
|
|
73
|
-
const x = every(map((v) =>
|
|
73
|
+
const x = every(map((v) => has(v)(universe))(countdown(256)));
|
|
74
74
|
if (!x) {
|
|
75
75
|
throw x;
|
|
76
76
|
}
|
|
77
77
|
},
|
|
78
78
|
compliment: {
|
|
79
79
|
empty: () => {
|
|
80
|
-
const r =
|
|
81
|
-
if (r !==
|
|
80
|
+
const r = complement(empty);
|
|
81
|
+
if (r !== universe) {
|
|
82
82
|
throw r;
|
|
83
83
|
}
|
|
84
84
|
},
|
|
85
85
|
universe: () => {
|
|
86
|
-
const r =
|
|
87
|
-
if (r !==
|
|
86
|
+
const r = complement(universe);
|
|
87
|
+
if (r !== empty) {
|
|
88
88
|
throw r;
|
|
89
89
|
}
|
|
90
90
|
},
|
|
91
91
|
},
|
|
92
92
|
toRangeMap: [
|
|
93
93
|
() => {
|
|
94
|
-
const result = stringify(toArray(
|
|
94
|
+
const result = stringify(toArray(toRangeMap(empty)('a')));
|
|
95
95
|
if (result !== '[]') {
|
|
96
96
|
throw result;
|
|
97
97
|
}
|
|
98
98
|
},
|
|
99
99
|
() => {
|
|
100
|
-
const s =
|
|
101
|
-
const result = stringify(toArray(
|
|
100
|
+
const s = set(0)(empty);
|
|
101
|
+
const result = stringify(toArray(toRangeMap(s)('a')));
|
|
102
102
|
if (result !== '[[["a"],0]]') {
|
|
103
103
|
throw result;
|
|
104
104
|
}
|
|
105
105
|
},
|
|
106
106
|
() => {
|
|
107
|
-
const s =
|
|
108
|
-
const result = stringify(toArray(
|
|
107
|
+
const s = setRange([1, 2])(empty);
|
|
108
|
+
const result = stringify(toArray(toRangeMap(s)('a')));
|
|
109
109
|
if (result !== '[[[],0],[["a"],2]]') {
|
|
110
110
|
throw result;
|
|
111
111
|
}
|
|
112
112
|
},
|
|
113
113
|
() => {
|
|
114
|
-
const result = stringify(toArray(
|
|
114
|
+
const result = stringify(toArray(toRangeMap(universe)('a')));
|
|
115
115
|
if (result !== '[[["a"],255]]') {
|
|
116
116
|
throw result;
|
|
117
117
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { get, merge, fromRange } from "./module.f.js";
|
|
2
2
|
import { unsafeCmp } from "../function/compare/module.f.js";
|
|
3
3
|
import * as json from "../../json/module.f.js";
|
|
4
4
|
import { sort } from "../object/module.f.js";
|
|
@@ -12,7 +12,7 @@ export default {
|
|
|
12
12
|
() => {
|
|
13
13
|
const a = [[['a'], 1], [['b'], 2]];
|
|
14
14
|
const b = null;
|
|
15
|
-
const merged =
|
|
15
|
+
const merged = merge(op)(a)(b);
|
|
16
16
|
const result = stringify(list.toArray(merged));
|
|
17
17
|
if (result !== '[[["a"],1],[["b"],2]]') {
|
|
18
18
|
throw result;
|
|
@@ -21,7 +21,7 @@ export default {
|
|
|
21
21
|
() => {
|
|
22
22
|
const a = null;
|
|
23
23
|
const b = [[['a'], 1], [['b'], 2]];
|
|
24
|
-
const merged =
|
|
24
|
+
const merged = merge(op)(a)(b);
|
|
25
25
|
const result = stringify(list.toArray(merged));
|
|
26
26
|
if (result !== '[[["a"],1],[["b"],2]]') {
|
|
27
27
|
throw result;
|
|
@@ -30,7 +30,7 @@ export default {
|
|
|
30
30
|
() => {
|
|
31
31
|
const a = [[['a'], 1], [['b'], 2]];
|
|
32
32
|
const b = [[['a'], 1], [['b'], 2]];
|
|
33
|
-
const merged =
|
|
33
|
+
const merged = merge(op)(a)(b);
|
|
34
34
|
const result = stringify(list.toArray(merged));
|
|
35
35
|
if (result !== '[[["a"],1],[["b"],2]]') {
|
|
36
36
|
throw result;
|
|
@@ -39,7 +39,7 @@ export default {
|
|
|
39
39
|
() => {
|
|
40
40
|
const a = [[['a'], 1], [['c'], 3]];
|
|
41
41
|
const b = [[['b'], 2], [['d'], 4]];
|
|
42
|
-
const merged =
|
|
42
|
+
const merged = merge(op)(a)(b);
|
|
43
43
|
const result = stringify(list.toArray(merged));
|
|
44
44
|
if (result !== '[[["a","b"],1],[["b","c"],2],[["c","d"],3],[["d"],4]]') {
|
|
45
45
|
throw result;
|
|
@@ -48,7 +48,7 @@ export default {
|
|
|
48
48
|
() => {
|
|
49
49
|
const a = [[['a'], 1], [['d'], 4]];
|
|
50
50
|
const b = [[['b'], 2], [['c'], 3]];
|
|
51
|
-
const merged =
|
|
51
|
+
const merged = merge(op)(a)(b);
|
|
52
52
|
const result = stringify(list.toArray(merged));
|
|
53
53
|
if (result !== '[[["a","b"],1],[["b","d"],2],[["c","d"],3],[["d"],4]]') {
|
|
54
54
|
throw result;
|
|
@@ -57,7 +57,7 @@ export default {
|
|
|
57
57
|
() => {
|
|
58
58
|
const a = [[['a'], 1], [['b'], 2]];
|
|
59
59
|
const b = [[['b'], 1], [['a'], 2]];
|
|
60
|
-
const merged =
|
|
60
|
+
const merged = merge(op)(a)(b);
|
|
61
61
|
const result = stringify(list.toArray(merged));
|
|
62
62
|
if (result !== '[[["a","b"],2]]') {
|
|
63
63
|
throw result;
|
|
@@ -66,7 +66,7 @@ export default {
|
|
|
66
66
|
() => {
|
|
67
67
|
const a = [[['a'], 1], [['b'], 2], [['a'], 3]];
|
|
68
68
|
const b = [[['a'], 5]];
|
|
69
|
-
const merged =
|
|
69
|
+
const merged = merge(op)(a)(b);
|
|
70
70
|
const result = stringify(list.toArray(merged));
|
|
71
71
|
if (result !== '[[["a"],1],[["a","b"],2],[["a"],5]]') {
|
|
72
72
|
throw result;
|
|
@@ -75,60 +75,60 @@ export default {
|
|
|
75
75
|
],
|
|
76
76
|
get: () => {
|
|
77
77
|
const sortedSetEmpty = [];
|
|
78
|
-
const
|
|
78
|
+
const at = get(sortedSetEmpty);
|
|
79
79
|
return [
|
|
80
80
|
() => {
|
|
81
81
|
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
|
|
82
|
-
const result = stringify(
|
|
82
|
+
const result = stringify(at(5)(rm));
|
|
83
83
|
if (result !== '["a"]') {
|
|
84
84
|
throw result;
|
|
85
85
|
}
|
|
86
86
|
},
|
|
87
87
|
() => {
|
|
88
88
|
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
|
|
89
|
-
const result = stringify(
|
|
89
|
+
const result = stringify(at(10)(rm));
|
|
90
90
|
if (result !== '["a"]') {
|
|
91
91
|
throw result;
|
|
92
92
|
}
|
|
93
93
|
},
|
|
94
94
|
() => {
|
|
95
95
|
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
|
|
96
|
-
const result = stringify(
|
|
96
|
+
const result = stringify(at(15)(rm));
|
|
97
97
|
if (result !== '["b"]') {
|
|
98
98
|
throw result;
|
|
99
99
|
}
|
|
100
100
|
},
|
|
101
101
|
() => {
|
|
102
102
|
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
|
|
103
|
-
const result = stringify(
|
|
103
|
+
const result = stringify(at(20)(rm));
|
|
104
104
|
if (result !== '["b"]') {
|
|
105
105
|
throw result;
|
|
106
106
|
}
|
|
107
107
|
},
|
|
108
108
|
() => {
|
|
109
109
|
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
|
|
110
|
-
const result = stringify(
|
|
110
|
+
const result = stringify(at(25)(rm));
|
|
111
111
|
if (result !== '["c"]') {
|
|
112
112
|
throw result;
|
|
113
113
|
}
|
|
114
114
|
},
|
|
115
115
|
() => {
|
|
116
116
|
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
|
|
117
|
-
const result = stringify(
|
|
117
|
+
const result = stringify(at(30)(rm));
|
|
118
118
|
if (result !== '["c"]') {
|
|
119
119
|
throw result;
|
|
120
120
|
}
|
|
121
121
|
},
|
|
122
122
|
() => {
|
|
123
123
|
const rm = [[['a'], 10], [['b'], 20], [['c'], 30]];
|
|
124
|
-
const result = stringify(
|
|
124
|
+
const result = stringify(at(35)(rm));
|
|
125
125
|
if (result !== '[]') {
|
|
126
126
|
throw result;
|
|
127
127
|
}
|
|
128
128
|
},
|
|
129
129
|
() => {
|
|
130
130
|
const rm = [];
|
|
131
|
-
const result = stringify(
|
|
131
|
+
const result = stringify(at(10)(rm));
|
|
132
132
|
if (result !== '[]') {
|
|
133
133
|
throw result;
|
|
134
134
|
}
|
|
@@ -137,34 +137,34 @@ export default {
|
|
|
137
137
|
},
|
|
138
138
|
fromRange: () => {
|
|
139
139
|
const def = -1;
|
|
140
|
-
const rm =
|
|
140
|
+
const rm = fromRange(def)([1, 7])(42);
|
|
141
141
|
return [
|
|
142
142
|
() => {
|
|
143
|
-
const result =
|
|
143
|
+
const result = get(def)(0)(rm);
|
|
144
144
|
if (result !== -1) {
|
|
145
145
|
throw result;
|
|
146
146
|
}
|
|
147
147
|
},
|
|
148
148
|
() => {
|
|
149
|
-
const result =
|
|
149
|
+
const result = get(def)(1)(rm);
|
|
150
150
|
if (result !== 42) {
|
|
151
151
|
throw result;
|
|
152
152
|
}
|
|
153
153
|
},
|
|
154
154
|
() => {
|
|
155
|
-
const result =
|
|
155
|
+
const result = get(def)(3)(rm);
|
|
156
156
|
if (result !== 42) {
|
|
157
157
|
throw result;
|
|
158
158
|
}
|
|
159
159
|
},
|
|
160
160
|
() => {
|
|
161
|
-
const result =
|
|
161
|
+
const result = get(def)(7)(rm);
|
|
162
162
|
if (result !== 42) {
|
|
163
163
|
throw result;
|
|
164
164
|
}
|
|
165
165
|
},
|
|
166
166
|
() => {
|
|
167
|
-
const result =
|
|
167
|
+
const result = get(def)(9)(rm);
|
|
168
168
|
if (result !== -1) {
|
|
169
169
|
throw result;
|
|
170
170
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { next } from "../list/module.f.js";
|
|
2
2
|
import { identity } from "../function/module.f.js";
|
|
3
3
|
export const genericMerge = ({ reduceOp, tailReduce }) => {
|
|
4
|
-
const f = state => a => b => () => {
|
|
4
|
+
const f = (state) => (a) => (b) => () => {
|
|
5
5
|
const aResult = next(a);
|
|
6
6
|
if (aResult === null) {
|
|
7
7
|
return tailReduce(state)(b);
|
|
@@ -22,12 +22,12 @@ export const merge = (cmp) => {
|
|
|
22
22
|
const tailReduce = mergeTail;
|
|
23
23
|
return genericMerge({ reduceOp: cmpReduce(cmp), tailReduce })(null);
|
|
24
24
|
};
|
|
25
|
-
const cmpReduce = cmp => () => a => b => {
|
|
25
|
+
const cmpReduce = (cmp) => () => a => b => {
|
|
26
26
|
const sign = cmp(a)(b);
|
|
27
27
|
return [sign === 1 ? b : a, sign, null];
|
|
28
28
|
};
|
|
29
29
|
const mergeTail = () => identity;
|
|
30
|
-
export const find = cmp => value => array => {
|
|
30
|
+
export const find = (cmp) => (value) => (array) => {
|
|
31
31
|
let b = 0;
|
|
32
32
|
let e = array.length - 1;
|
|
33
33
|
while (true) {
|
|
@@ -1,3 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A sorted set, implemented as a sorted array.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*
|
|
6
|
+
* @note
|
|
7
|
+
*
|
|
8
|
+
* All input arrays must be pre-sorted according to the provided comparison function (`Cmp<T>`).
|
|
9
|
+
* The correctness of these functions depend on this requirement.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```js
|
|
14
|
+
* import { union, intersect, has } from './module.f.ts'
|
|
15
|
+
*
|
|
16
|
+
* const cmp = (a: number) => (b: number) => a < b ? -1 : a > b ? 1 : 0
|
|
17
|
+
*
|
|
18
|
+
* const setA = [1, 3, 5]
|
|
19
|
+
* const setB = [3, 4, 5]
|
|
20
|
+
*
|
|
21
|
+
* const unionSet = union(cmp)(setA)(setB) // [1, 3, 4, 5]
|
|
22
|
+
*
|
|
23
|
+
* const intersectionSet = intersect(cmp)(setA)(setB) // [3, 5]
|
|
24
|
+
*
|
|
25
|
+
* has(cmp)(3)(setA) // true
|
|
26
|
+
* has(cmp)(2)(setA) // false
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
1
29
|
import type { Sign } from '../function/compare/module.f.ts';
|
|
2
30
|
export type SortedSet<T> = readonly T[];
|
|
3
31
|
type Cmp<T> = (a: T) => (b: T) => Sign;
|
|
@@ -7,6 +7,25 @@ import { flip } from "../function/module.f.js";
|
|
|
7
7
|
const stringify = a => json.stringify(sort)(a);
|
|
8
8
|
const reverseCmp = flip(unsafeCmp);
|
|
9
9
|
export default {
|
|
10
|
+
example: () => {
|
|
11
|
+
const cmp = (a) => (b) => a < b ? -1 : a > b ? 1 : 0;
|
|
12
|
+
const setA = [1, 3, 5];
|
|
13
|
+
const setB = [3, 4, 5];
|
|
14
|
+
const unionSet = union(cmp)(setA)(setB); // [1, 3, 4, 5]
|
|
15
|
+
if (stringify(unionSet) !== '[1,3,4,5]') {
|
|
16
|
+
throw 0;
|
|
17
|
+
}
|
|
18
|
+
const intersectionSet = intersect(cmp)(setA)(setB); // [3, 5]
|
|
19
|
+
if (stringify(intersectionSet) !== '[3,5]') {
|
|
20
|
+
throw 1;
|
|
21
|
+
}
|
|
22
|
+
if (!has(cmp)(3)(setA)) {
|
|
23
|
+
throw 2;
|
|
24
|
+
}
|
|
25
|
+
if (has(cmp)(2)(setA)) {
|
|
26
|
+
throw 3;
|
|
27
|
+
}
|
|
28
|
+
},
|
|
10
29
|
union: [
|
|
11
30
|
() => {
|
|
12
31
|
const result = stringify(toArray(union(unsafeCmp)([2, 3, 4])([1, 3, 5])));
|
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for working with strings and lists of strings.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { join, concat, repeat, cmp } from './module.f.ts'
|
|
10
|
+
*
|
|
11
|
+
* const words = ['hello', 'world']
|
|
12
|
+
* join(' ')(words) // 'hello world'
|
|
13
|
+
* concat(words) // 'helloworld'
|
|
14
|
+
* repeat('abc')(3) // 'abcabcabc'
|
|
15
|
+
* cmp('apple')('banana') // -1
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
1
18
|
import { type List } from '../list/module.f.ts';
|
|
2
19
|
import { type Sign } from '../function/compare/module.f.ts';
|
|
3
20
|
export declare const join: (_: string) => (input: List<string>) => string;
|
package/types/string/module.f.js
CHANGED
|
@@ -1,3 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility functions for working with strings and lists of strings.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { join, concat, repeat, cmp } from './module.f.ts'
|
|
10
|
+
*
|
|
11
|
+
* const words = ['hello', 'world']
|
|
12
|
+
* join(' ')(words) // 'hello world'
|
|
13
|
+
* concat(words) // 'helloworld'
|
|
14
|
+
* repeat('abc')(3) // 'abcabcabc'
|
|
15
|
+
* cmp('apple')('banana') // -1
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
1
18
|
import { reduce as listReduce, repeat as listRepeat } from "../list/module.f.js";
|
|
2
19
|
import { compose } from "../function/module.f.js";
|
|
3
20
|
import { unsafeCmp } from "../function/compare/module.f.js";
|
package/types/string/test.f.d.ts
CHANGED
package/types/string/test.f.js
CHANGED
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
import { join, concat, repeat, cmp } from "./module.f.js";
|
|
2
2
|
import { repeat as repeatList } from "../list/module.f.js";
|
|
3
3
|
export default {
|
|
4
|
+
example: () => {
|
|
5
|
+
const words = ['hello', 'world'];
|
|
6
|
+
if (join(' ')(words) !== 'hello world') {
|
|
7
|
+
throw 0;
|
|
8
|
+
}
|
|
9
|
+
if (concat(words) !== 'helloworld') {
|
|
10
|
+
throw 1;
|
|
11
|
+
}
|
|
12
|
+
if (repeat('abc')(3) !== 'abcabcabc') {
|
|
13
|
+
throw 2;
|
|
14
|
+
}
|
|
15
|
+
if (cmp('apple')('banana') !== -1) {
|
|
16
|
+
throw 3;
|
|
17
|
+
}
|
|
18
|
+
},
|
|
4
19
|
join: {
|
|
5
20
|
0: () => {
|
|
6
21
|
const result = join('/')([]);
|
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A set of strings implemented as a B-Tree.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { set, contains, remove, fromValues, values, empty } from './module.d.ts';
|
|
10
|
+
*
|
|
11
|
+
* let mySet = fromValues(['apple', 'banana', 'cherry']);
|
|
12
|
+
* if (!contains('banana')(mySet)) { throw '1' }
|
|
13
|
+
* if (contains('date')(mySet)) { throw '2' }
|
|
14
|
+
*
|
|
15
|
+
* mySet = set('date')(mySet);
|
|
16
|
+
* if (!contains('date')(mySet)) { throw '3' }
|
|
17
|
+
*
|
|
18
|
+
* mySet = remove('banana')(mySet);
|
|
19
|
+
* if (contains('banana')(mySet)) { throw '4' }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
1
22
|
import type { Tree } from '../btree/types/module.f.ts';
|
|
2
23
|
import { type List } from '../list/module.f.ts';
|
|
3
24
|
export declare const values: (s: StringSet) => List<string>;
|
|
@@ -1,3 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A set of strings implemented as a B-Tree.
|
|
3
|
+
*
|
|
4
|
+
* @module
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
*
|
|
8
|
+
* ```js
|
|
9
|
+
* import { set, contains, remove, fromValues, values, empty } from './module.d.ts';
|
|
10
|
+
*
|
|
11
|
+
* let mySet = fromValues(['apple', 'banana', 'cherry']);
|
|
12
|
+
* if (!contains('banana')(mySet)) { throw '1' }
|
|
13
|
+
* if (contains('date')(mySet)) { throw '2' }
|
|
14
|
+
*
|
|
15
|
+
* mySet = set('date')(mySet);
|
|
16
|
+
* if (!contains('date')(mySet)) { throw '3' }
|
|
17
|
+
*
|
|
18
|
+
* mySet = remove('banana')(mySet);
|
|
19
|
+
* if (contains('banana')(mySet)) { throw '4' }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
1
22
|
import * as btree from "../btree/module.f.js";
|
|
2
23
|
import { find, isFound } from "../btree/find/module.f.js";
|
|
3
24
|
import { remove as btreeRemove } from "../btree/remove/module.f.js";
|
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
import { contains, remove, set } from "./module.f.js";
|
|
1
|
+
import { contains, fromValues, remove, set } from "./module.f.js";
|
|
2
2
|
export default {
|
|
3
|
+
example: () => {
|
|
4
|
+
let mySet = fromValues(['apple', 'banana', 'cherry']);
|
|
5
|
+
if (!contains('banana')(mySet)) {
|
|
6
|
+
throw '1';
|
|
7
|
+
}
|
|
8
|
+
if (contains('date')(mySet)) {
|
|
9
|
+
throw '2';
|
|
10
|
+
}
|
|
11
|
+
mySet = set('date')(mySet);
|
|
12
|
+
if (!contains('date')(mySet)) {
|
|
13
|
+
throw '3';
|
|
14
|
+
}
|
|
15
|
+
mySet = remove('banana')(mySet);
|
|
16
|
+
if (contains('banana')(mySet)) {
|
|
17
|
+
throw '4';
|
|
18
|
+
}
|
|
19
|
+
},
|
|
3
20
|
contains: () => {
|
|
4
21
|
const r = set('hello')(null);
|
|
5
22
|
if (!contains('hello')(r)) {
|