@cgtk/std 0.0.197 → 0.0.199
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/array.d.ts +5 -0
- package/array.js +17 -0
- package/iterable.d.ts +2 -4
- package/iterable.js +6 -9
- package/package.json +1 -1
- package/scope.d.ts +0 -1
- package/scope.js +1 -3
- package/string.d.ts +1 -0
- package/string.js +2 -0
- package/treemap.js +5 -9
- package/typedarray.d.ts +1 -1
- package/typedarray.js +5 -5
package/array.d.ts
CHANGED
|
@@ -13,3 +13,8 @@ export declare const tuple: <T, N extends number>(...xs: T[] & {
|
|
|
13
13
|
length: N;
|
|
14
14
|
}) => Tuple<T, N>;
|
|
15
15
|
export declare const seek: <T>(arr: ArrayLike<T>, vals: Set<T | number>, offset?: number) => number;
|
|
16
|
+
export declare const unique: <T>(vals: T[], compareFn?: (a: T, b: T) => number) => {
|
|
17
|
+
indices: number[];
|
|
18
|
+
inverse: number[];
|
|
19
|
+
};
|
|
20
|
+
export declare const argsort: <T>(arr: T[], compareFn?: (a: T, b: T) => number) => number[];
|
package/array.js
CHANGED
|
@@ -20,3 +20,20 @@ export const seek = (arr, vals, offset = 0) => {
|
|
|
20
20
|
return i;
|
|
21
21
|
return -1;
|
|
22
22
|
};
|
|
23
|
+
const compare = (a, b) => a < b ? -1 : a > b ? 1 : 0;
|
|
24
|
+
export const unique = (vals, compareFn = compare) => {
|
|
25
|
+
const pairs = vals.map((x, i) => [x, i]);
|
|
26
|
+
pairs.sort((a, b) => compareFn(a[0], b[0]));
|
|
27
|
+
const indices = [], inverse = Array.from({ length: pairs.length });
|
|
28
|
+
let k = -1;
|
|
29
|
+
pairs.forEach((x, i) => {
|
|
30
|
+
if (i == 0 || x[0] != pairs[i - 1][0])
|
|
31
|
+
indices.push(x[1]), k++;
|
|
32
|
+
inverse[x[1]] = k;
|
|
33
|
+
});
|
|
34
|
+
return { indices, inverse };
|
|
35
|
+
};
|
|
36
|
+
export const argsort = (arr, compareFn = compare) => arr
|
|
37
|
+
.map((x, i) => [x, i])
|
|
38
|
+
.sort((a, b) => compareFn(a[0], b[0]))
|
|
39
|
+
.map(x => x[1]);
|
package/iterable.d.ts
CHANGED
|
@@ -5,24 +5,22 @@ export declare const just: <T>(x: T) => IteratorObject<T>;
|
|
|
5
5
|
export declare const enumerate: (n?: number) => <T>(x: T) => Indexed<T>;
|
|
6
6
|
export declare function empty(): Generator<never, void, unknown>;
|
|
7
7
|
export declare function concat<T>(...xxs: ReadonlyArray<IteratorObject<T>>): Generator<T, void, unknown>;
|
|
8
|
-
export declare const map: <T, U = T>(f: Fn<T, U>) => (xs: IteratorObject<T>) => IteratorObject<U, undefined, unknown>;
|
|
9
8
|
export declare const prepend: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
|
10
9
|
export declare const append: <A>(ys: IteratorObject<A>) => (xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
|
11
10
|
export declare const first: <T>(xs: IteratorObject<T>) => T | undefined;
|
|
12
|
-
export declare const reduce: <T, S>(f: FnV<[S, T, number], S>, a: S) => (xs: IteratorObject<T>) => S;
|
|
13
11
|
export declare const reduceRec: <T, A, R>(xs: IteratorObject<T>, f: FnV<[A, T, Fn<A>], R>, o: A, result: Fn<A, R>) => R;
|
|
14
12
|
export declare const take: <T>(n: number, xs: IteratorObject<T>) => Generator<T, void, unknown>;
|
|
15
13
|
export declare function arange(a: number, b?: number, s?: number): Generator<number, void, unknown>;
|
|
16
14
|
export declare const linspace: (a: number, b: number, n: number) => Generator<number, void, unknown>;
|
|
17
15
|
export declare const steps: (a: number, b: number, s?: number, o?: number, eps?: number) => Generator<number, void, unknown>;
|
|
18
|
-
export declare const strided: (n: number, s: number
|
|
16
|
+
export declare const strided: <T>(n: number, s: number, xs: Iterable<T>) => Generator<T[], void, unknown>;
|
|
19
17
|
export declare const stopAt: <T>(p: Pred<T>) => (xs: IteratorObject<T>) => Generator<T, void, unknown>;
|
|
20
18
|
export declare const until: (p: Pred<void>) => <A>(xs: IteratorObject<A>) => Generator<A, void, unknown>;
|
|
21
19
|
export type Iterators<T extends any[]> = {
|
|
22
20
|
[k in keyof T]: IteratorObject<T[k]>;
|
|
23
21
|
};
|
|
24
22
|
export declare function zip<T extends unknown[]>(...xs: Iterators<T>): Generator<T>;
|
|
25
|
-
export declare const iterA: <T>(xs: Promise<T
|
|
23
|
+
export declare const iterA: <T>(xs: Iterable<Promise<T>>) => AsyncGenerator<Awaited<T>, void, unknown>;
|
|
26
24
|
export declare const forEachA: <T>(f?: Fn<T>) => (xs: AsyncIterable<T>) => Promise<void>;
|
|
27
25
|
export declare const mapA: <A, B>(f: Fn<A, B>) => Fn<AsyncIterator<A>, AsyncIterableIterator<B>>;
|
|
28
26
|
export declare const flatMapA: <A, B>(f: Fn<A, Iterable<B>>) => (xs: AsyncIterator<A>) => AsyncGenerator<Awaited<B>, void, any>;
|
package/iterable.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { assert, isDefined } from "./checks.js";
|
|
2
|
-
import {
|
|
2
|
+
import { scan, apply, noop, constantly } from "./fn.js";
|
|
3
3
|
import { set } from "./map.js";
|
|
4
4
|
export const next = (xs) => {
|
|
5
5
|
const { done, value } = xs.next();
|
|
@@ -15,11 +15,9 @@ export function* empty() { }
|
|
|
15
15
|
export function* concat(...xxs) { for (const xs of xxs)
|
|
16
16
|
yield* xs; }
|
|
17
17
|
;
|
|
18
|
-
export const map = (f) => (xs) => xs.map(f);
|
|
19
18
|
export const prepend = (ys) => (xs) => concat(ys, xs);
|
|
20
19
|
export const append = (ys) => (xs) => concat(xs, ys);
|
|
21
20
|
export const first = (xs) => xs.find(constantly(true));
|
|
22
|
-
export const reduce = (f, a) => (xs) => xs.reduce(f, a);
|
|
23
21
|
export const reduceRec = (xs, f, o, result) => {
|
|
24
22
|
const next = (o) => {
|
|
25
23
|
const { done, value } = xs.next();
|
|
@@ -50,7 +48,7 @@ export const steps = (a, b, s = 1, o = 0, eps = 1e-8) => {
|
|
|
50
48
|
const end = Math.floor((b - o) / s) * s + o;
|
|
51
49
|
return arange(start, end + s - eps, s);
|
|
52
50
|
};
|
|
53
|
-
export const strided = (n, s
|
|
51
|
+
export const strided = function* (n, s, xs) {
|
|
54
52
|
const cur = [];
|
|
55
53
|
for (const x of xs) {
|
|
56
54
|
cur.push(x);
|
|
@@ -134,15 +132,14 @@ export const reduceA = (f, a) => async (xs) => {
|
|
|
134
132
|
return a;
|
|
135
133
|
};
|
|
136
134
|
export const race = (n) => async function* (xs) {
|
|
137
|
-
const it =
|
|
138
|
-
const
|
|
139
|
-
const tasks =
|
|
140
|
-
const next = comp(bind((take), 1), accum);
|
|
135
|
+
const it = xs.map((x, i) => [i, x.then(y => [i, y])]);
|
|
136
|
+
const acc = new Map();
|
|
137
|
+
const tasks = take(n, it).reduce(set, acc);
|
|
141
138
|
while (tasks.size > 0) {
|
|
142
139
|
const [i, x] = await Promise.race(tasks.values());
|
|
143
140
|
tasks.delete(i);
|
|
144
141
|
yield x;
|
|
145
|
-
|
|
142
|
+
take(1, it).reduce(set, acc);
|
|
146
143
|
}
|
|
147
144
|
};
|
|
148
145
|
export const zmap = (f) => (...xs) => zip(...xs).forEach(apply(f));
|
package/package.json
CHANGED
package/scope.d.ts
CHANGED
|
@@ -14,6 +14,5 @@ export declare const compose: <R extends Structure<Scope<any>>>(rs: R) => Scope<
|
|
|
14
14
|
export declare const of: <T>(x: T, f: Fn<T, Dispose>) => Scope<T>;
|
|
15
15
|
type Use = <T>(r: Scope<T>) => T;
|
|
16
16
|
export declare const gen: <R>(f: FnV<[Use, Fn<Dispose, Dispose>], R>) => Scope<R>;
|
|
17
|
-
export declare const consume: (f: Fn<Use, Dispose | void>) => Dispose;
|
|
18
17
|
export declare const worker: (w: Worker) => Scope<Worker>;
|
|
19
18
|
export {};
|
package/scope.js
CHANGED
|
@@ -29,13 +29,11 @@ export const compose = (rs) => {
|
|
|
29
29
|
}
|
|
30
30
|
return Object.fromEntries(Object.entries(rs).map(([k, v]) => [k, rec(v)]));
|
|
31
31
|
};
|
|
32
|
-
|
|
33
|
-
return scope(value, F.forEach(...disposes));
|
|
32
|
+
return scope(rec(rs), F.forEach(...disposes));
|
|
34
33
|
};
|
|
35
34
|
export const of = (x, f) => scope(x, f(x));
|
|
36
35
|
export const gen = (f) => {
|
|
37
36
|
const res = new Set();
|
|
38
37
|
return scope(f(r => (res.add(r.dispose), r.value), F.bind(add, res)), () => res.forEach(f => f()));
|
|
39
38
|
};
|
|
40
|
-
export const consume = (f) => F.disposable(use => use(f(r => (use(r.dispose), r.value)) ?? F.noop));
|
|
41
39
|
export const worker = (w) => of(w, x => x.terminate.bind(x));
|
package/string.d.ts
CHANGED
|
@@ -7,3 +7,4 @@ export declare const decode: (label?: string, opts?: TextDecoderOptions) => (x:
|
|
|
7
7
|
export declare const readLine: (delims?: Set<number>) => (data: Uint8Array, offset?: number) => string;
|
|
8
8
|
export declare const DATA_URL: RegExp;
|
|
9
9
|
export declare const came2kebab: (s: string) => string;
|
|
10
|
+
export declare const join: (sep: string, ...xs: Array<string | undefined>) => string;
|
package/string.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { EMPTY } from "./constants.js";
|
|
2
2
|
import { seek } from "./array.js";
|
|
3
|
+
import { isDefined } from "./checks.js";
|
|
3
4
|
export const basename = (path) => path.split("/").at(-1) ?? "";
|
|
4
5
|
export const dirname = (path) => {
|
|
5
6
|
const parts = path.split("/");
|
|
@@ -28,3 +29,4 @@ export const readLine = (delims = new Set([0x0A])) => {
|
|
|
28
29
|
};
|
|
29
30
|
export const DATA_URL = /^data:(?<mime>[\w/\-\+]+)?(;(?<params>[\w\-]+\=[^;,\s]+)*)?(;(?<encoding>base64))?,(?<data>.*)$/;
|
|
30
31
|
export const came2kebab = (s) => s.replace(/[A-Z]/g, m => '-' + m.toLowerCase());
|
|
32
|
+
export const join = (sep, ...xs) => xs.filter(isDefined).join(sep);
|
package/treemap.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { before } from "./fn.js";
|
|
2
2
|
import { isDefined } from "./checks.js";
|
|
3
|
-
import { EMPTY } from "./constants.js";
|
|
4
3
|
export const treemap = () => {
|
|
5
4
|
const nodes = new Set();
|
|
6
5
|
const parents = new Map();
|
|
@@ -10,20 +9,17 @@ export const treemap = () => {
|
|
|
10
9
|
const hasChanged = changed;
|
|
11
10
|
if (hasChanged) {
|
|
12
11
|
children.clear();
|
|
13
|
-
|
|
12
|
+
nodes.forEach(k => {
|
|
14
13
|
const p = parents.get(k);
|
|
15
|
-
if (isDefined(p))
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
children.get(p).push(k);
|
|
19
|
-
}
|
|
20
|
-
}
|
|
14
|
+
if (isDefined(p))
|
|
15
|
+
children.getOrInsert(p, []).push(k);
|
|
16
|
+
});
|
|
21
17
|
changed = false;
|
|
22
18
|
}
|
|
23
19
|
return hasChanged;
|
|
24
20
|
};
|
|
25
21
|
const updated = before(update);
|
|
26
|
-
const getChildren = updated((key) => children.
|
|
22
|
+
const getChildren = updated((key) => children.getOrInsert(key, []));
|
|
27
23
|
const parent = updated((key) => parents.get(key));
|
|
28
24
|
const roots = () => nodes.values().filter(k => !parents.has(k));
|
|
29
25
|
return {
|
package/typedarray.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ export declare const ctor: <T extends TypedArray>(x: T) => Constructor<T>;
|
|
|
24
24
|
export declare const emptyLike: <T extends TypedArray>(x: T) => T;
|
|
25
25
|
export declare const set: <T extends TypedArray>(xs: T, x: ArrayLike<Number<T>> | T, i: number) => T;
|
|
26
26
|
export declare const concat: <T extends TypedArray>(chunks: T[]) => T;
|
|
27
|
-
export declare const iter:
|
|
27
|
+
export declare const iter: <T extends TypedArray>(data: T, size: number, stride?: number) => Generator<T>;
|
|
28
28
|
export declare const tile: (n: number) => (out: TypedArray, x: TypedArray) => TypedArray;
|
|
29
29
|
export declare const tileEvery: (m: number, n: number) => <T extends TypedArray>(xs: T, out?: T) => T;
|
|
30
30
|
export declare const strided: <T extends TypedArray>(data: T, size: number, stride?: number, out?: T) => T;
|
package/typedarray.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { bind } from "./fn.js";
|
|
2
|
-
import { zmap
|
|
2
|
+
import { zmap } from "./iterable.js";
|
|
3
3
|
import { push } from "./array.js";
|
|
4
4
|
export const view = (type, length) => (buf = new ArrayBuffer(type.BYTES_PER_ELEMENT * length), offset = 0) => new type(buf, offset, length);
|
|
5
5
|
export const u8 = bind((view), Uint8Array);
|
|
@@ -22,7 +22,7 @@ export const concat = (chunks) => {
|
|
|
22
22
|
chunks.reduce((offset, chunk) => (out.set(chunk, offset), offset + chunk.length), 0);
|
|
23
23
|
return out;
|
|
24
24
|
};
|
|
25
|
-
export const iter = (size, stride = size)
|
|
25
|
+
export const iter = function* (data, size, stride = size) {
|
|
26
26
|
const C = ctor(data);
|
|
27
27
|
for (let i = 0, n = data.length; i < n - size + 1; i += stride) {
|
|
28
28
|
const offset = data.byteOffset + i * C.BYTES_PER_ELEMENT;
|
|
@@ -34,6 +34,6 @@ export const tile = (n) => (out, x) => {
|
|
|
34
34
|
set(out, x, i * x.length);
|
|
35
35
|
return out;
|
|
36
36
|
};
|
|
37
|
-
export const tileEvery = (m, n) => (xs, out = new (ctor(xs))(xs.length * n)) => (zmap(tile(n))(iter(m * n)
|
|
38
|
-
export const strided = (data, size, stride = size, out = new (ctor(data))(data.length / size * stride)) => (iter(size)
|
|
39
|
-
export const toFixed = (n, sep = ',') => (xs) =>
|
|
37
|
+
export const tileEvery = (m, n) => (xs, out = new (ctor(xs))(xs.length * n)) => (zmap(tile(n))(iter(out, m * n), iter(xs, m)), out);
|
|
38
|
+
export const strided = (data, size, stride = size, out = new (ctor(data))(data.length / size * stride)) => (iter(data, size).forEach((p, i) => set(out, p, i * stride)), out);
|
|
39
|
+
export const toFixed = (n, sep = ',') => (xs) => Iterator.from(xs).map(x => x.toFixed(n)).reduce((push), []).join(sep);
|