@thi.ng/transducers 8.8.13 → 8.8.15
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/CHANGELOG.md +1 -1
- package/add.js +5 -2
- package/api.js +0 -1
- package/as-iterable.js +5 -8
- package/assoc-map.js +8 -4
- package/assoc-obj.js +8 -4
- package/auto-obj.js +9 -5
- package/benchmark.js +14 -13
- package/cat.js +15 -49
- package/choices.js +6 -19
- package/comp.js +6 -3
- package/compr.js +4 -25
- package/concat.js +7 -20
- package/conj.js +8 -4
- package/consume.js +6 -24
- package/converge.js +18 -16
- package/convolve.js +66 -54
- package/count.js +14 -8
- package/curve.js +12 -45
- package/cycle.js +15 -34
- package/dedupe.js +21 -21
- package/deep-transform.js +17 -73
- package/delayed.js +4 -10
- package/distinct.js +17 -16
- package/div.js +8 -4
- package/drop-nth.js +12 -9
- package/drop-while.js +13 -8
- package/drop.js +12 -8
- package/dup.js +5 -6
- package/duplicate.js +13 -12
- package/ensure.js +4 -1
- package/every.js +13 -9
- package/extend-sides.js +15 -40
- package/fill.js +16 -9
- package/filter-fuzzy.js +13 -8
- package/filter.js +8 -7
- package/flatten-with.js +21 -20
- package/flatten.js +8 -2
- package/flatten1.js +5 -2
- package/frequencies.js +5 -3
- package/group-binary.js +15 -84
- package/group-by-map.js +25 -21
- package/group-by-obj.js +23 -22
- package/index.js +0 -4
- package/indexed.js +10 -7
- package/interleave.js +12 -11
- package/internal/drain.js +8 -15
- package/internal/group-opts.js +7 -11
- package/internal/mathop.js +10 -17
- package/internal/sort-opts.js +7 -11
- package/interpolate-hermite.js +10 -2
- package/interpolate-linear.js +10 -2
- package/interpolate.js +8 -4
- package/interpose.js +17 -16
- package/iterate.js +8 -28
- package/iterator.js +36 -62
- package/juxtr.js +74 -71
- package/keep.js +12 -7
- package/key-permutations.js +7 -30
- package/key-selector.js +4 -1
- package/keys.js +8 -16
- package/labeled.js +5 -4
- package/last.js +5 -2
- package/length.js +7 -6
- package/line.js +6 -29
- package/lookup.js +10 -44
- package/map-deep.js +5 -4
- package/map-indexed.js +10 -8
- package/map-keys.js +16 -13
- package/map-nth.js +34 -33
- package/map-vals.js +16 -13
- package/map.js +11 -13
- package/mapcat-indexed.js +5 -3
- package/mapcat.js +5 -2
- package/match-first.js +5 -4
- package/match-last.js +5 -4
- package/max-compare.js +11 -8
- package/max-mag.js +8 -4
- package/max.js +8 -4
- package/mean.js +10 -9
- package/min-compare.js +11 -8
- package/min-mag.js +8 -4
- package/min-max.js +4 -5
- package/min.js +8 -4
- package/moving-average.js +17 -16
- package/moving-median.js +17 -9
- package/mul.js +5 -2
- package/multiplex-obj.js +14 -8
- package/multiplex.js +12 -2
- package/noop.js +4 -6
- package/norm-count.js +10 -7
- package/norm-frequencies-auto.js +21 -18
- package/norm-frequencies.js +8 -6
- package/norm-range.js +20 -45
- package/package.json +14 -12
- package/pad-last.js +20 -19
- package/pad-sides.js +4 -35
- package/page.js +5 -3
- package/pairs.js +8 -22
- package/palindrome.js +5 -6
- package/partition-by.js +34 -34
- package/partition-of.js +14 -13
- package/partition-sort.js +15 -7
- package/partition-sync.js +118 -119
- package/partition-time.js +12 -11
- package/partition-when.js +28 -27
- package/partition.js +45 -44
- package/peek.js +5 -2
- package/permutations.js +35 -58
- package/pluck.js +5 -4
- package/push-copy.js +7 -1
- package/push-sort.js +9 -8
- package/push.js +8 -4
- package/range-nd.js +7 -36
- package/range.js +47 -51
- package/range2d.js +25 -22
- package/range3d.js +30 -27
- package/rechunk.js +34 -31
- package/reduce.js +53 -63
- package/reduced.js +19 -12
- package/reductions.js +18 -17
- package/rename.js +27 -16
- package/renamer.js +46 -52
- package/repeat.js +7 -19
- package/repeatedly.js +7 -22
- package/repeatedly2d.js +8 -24
- package/repeatedly3d.js +10 -26
- package/reverse.js +9 -18
- package/run.js +10 -8
- package/sample.js +17 -11
- package/scan.js +26 -24
- package/select-keys.js +5 -4
- package/side-effect.js +4 -8
- package/sliding-window.js +22 -19
- package/some.js +13 -9
- package/sorted-keys.js +5 -11
- package/step.js +16 -66
- package/str.js +10 -8
- package/stream-shuffle.js +41 -40
- package/stream-sort.js +25 -22
- package/struct.js +16 -4
- package/sub.js +5 -2
- package/swizzle.js +5 -4
- package/symmetric.js +13 -23
- package/take-last.js +18 -17
- package/take-nth.js +12 -9
- package/take-while.js +13 -8
- package/take.js +12 -12
- package/throttle-time.js +11 -10
- package/throttle.js +9 -8
- package/toggle.js +12 -11
- package/trace.js +4 -1
- package/transduce.js +23 -19
- package/tween.js +33 -94
- package/vals.js +8 -16
- package/word-wrap.js +25 -22
- package/wrap-sides.js +15 -31
- package/zip.js +15 -12
package/CHANGELOG.md
CHANGED
package/add.js
CHANGED
package/api.js
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/as-iterable.js
CHANGED
package/assoc-map.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { reduce, reducer } from "./reduce.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
function assocMap(xs) {
|
|
3
|
+
return xs ? reduce(assocMap(), xs) : reducer(
|
|
4
|
+
() => /* @__PURE__ */ new Map(),
|
|
5
|
+
(acc, [k, v]) => acc.set(k, v)
|
|
6
|
+
);
|
|
6
7
|
}
|
|
8
|
+
export {
|
|
9
|
+
assocMap
|
|
10
|
+
};
|
package/assoc-obj.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { reduce, reducer } from "./reduce.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
function assocObj(xs) {
|
|
3
|
+
return xs ? reduce(assocObj(), xs) : reducer(
|
|
4
|
+
() => ({}),
|
|
5
|
+
(acc, [k, v]) => (acc[k] = v, acc)
|
|
6
|
+
);
|
|
6
7
|
}
|
|
8
|
+
export {
|
|
9
|
+
assocObj
|
|
10
|
+
};
|
package/auto-obj.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import { reduce, reducer } from "./reduce.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
function autoObj(prefix, xs) {
|
|
3
|
+
let id = 0;
|
|
4
|
+
return xs ? reduce(autoObj(prefix), xs) : reducer(
|
|
5
|
+
() => ({}),
|
|
6
|
+
(acc, v) => (acc[prefix + id++] = v, acc)
|
|
7
|
+
);
|
|
7
8
|
}
|
|
9
|
+
export {
|
|
10
|
+
autoObj
|
|
11
|
+
};
|
package/benchmark.js
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
import { isIterable } from "@thi.ng/checks/is-iterable";
|
|
2
2
|
import { compR } from "./compr.js";
|
|
3
3
|
import { iterator1 } from "./iterator.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
});
|
|
16
|
-
};
|
|
4
|
+
function benchmark(src) {
|
|
5
|
+
return isIterable(src) ? iterator1(benchmark(), src) : (rfn) => {
|
|
6
|
+
const r = rfn[2];
|
|
7
|
+
let prev = Date.now();
|
|
8
|
+
return compR(rfn, (acc, _) => {
|
|
9
|
+
const t = Date.now();
|
|
10
|
+
const x = t - prev;
|
|
11
|
+
prev = t;
|
|
12
|
+
return r(acc, x);
|
|
13
|
+
});
|
|
14
|
+
};
|
|
17
15
|
}
|
|
16
|
+
export {
|
|
17
|
+
benchmark
|
|
18
|
+
};
|
package/cat.js
CHANGED
|
@@ -1,53 +1,19 @@
|
|
|
1
1
|
import { compR } from "./compr.js";
|
|
2
2
|
import { ensureReduced, isReduced, unreduced } from "./reduced.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* early termination by wrapping its own result in {@link reduced}. E.g. this
|
|
12
|
-
* behavior allows a {@link mapcat} user functions to benefit from reduced
|
|
13
|
-
* results.
|
|
14
|
-
*
|
|
15
|
-
* Also see:
|
|
16
|
-
* - {@link concat}
|
|
17
|
-
* - {@link mapcat}
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```ts
|
|
21
|
-
* [...iterator(comp(map((x) => [x, x]), cat()), [1, 2, 3, 4])]
|
|
22
|
-
* // [ 1, 1, 2, 2, 3, 3, 4, 4 ]
|
|
23
|
-
*
|
|
24
|
-
* [...iterator(
|
|
25
|
-
* comp(
|
|
26
|
-
* mapIndexed((i, x) => [[i], [x, x]]),
|
|
27
|
-
* cat(),
|
|
28
|
-
* cat()
|
|
29
|
-
* ),
|
|
30
|
-
* "abc"
|
|
31
|
-
* )]
|
|
32
|
-
* // [ 0, 'a', 'a', 1, 'b', 'b', 2, 'c', 'c' ]
|
|
33
|
-
*
|
|
34
|
-
* [...mapcat((x)=>(x > 1 ? reduced([x, x]) : [x, x]), [1, 2, 3, 4])]
|
|
35
|
-
* // [ 1, 1, 2, 2 ]
|
|
36
|
-
* ```
|
|
37
|
-
*
|
|
38
|
-
* @param rfn -
|
|
39
|
-
*/
|
|
40
|
-
export const cat = () => (rfn) => {
|
|
41
|
-
const r = rfn[2];
|
|
42
|
-
return compR(rfn, (acc, x) => {
|
|
43
|
-
if (x) {
|
|
44
|
-
for (let y of unreduced(x)) {
|
|
45
|
-
acc = r(acc, y);
|
|
46
|
-
if (isReduced(acc)) {
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
3
|
+
const cat = () => (rfn) => {
|
|
4
|
+
const r = rfn[2];
|
|
5
|
+
return compR(rfn, (acc, x) => {
|
|
6
|
+
if (x) {
|
|
7
|
+
for (let y of unreduced(x)) {
|
|
8
|
+
acc = r(acc, y);
|
|
9
|
+
if (isReduced(acc)) {
|
|
10
|
+
break;
|
|
50
11
|
}
|
|
51
|
-
|
|
52
|
-
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return isReduced(x) ? ensureReduced(acc) : acc;
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
export {
|
|
18
|
+
cat
|
|
53
19
|
};
|
package/choices.js
CHANGED
|
@@ -2,22 +2,9 @@ import { ensureArray } from "@thi.ng/arrays/ensure-array";
|
|
|
2
2
|
import { SYSTEM } from "@thi.ng/random/system";
|
|
3
3
|
import { weightedRandom } from "@thi.ng/random/weighted-random";
|
|
4
4
|
import { repeatedly } from "./repeatedly.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
* ```ts
|
|
12
|
-
* transduce(take(1000), frequencies(), choices("abcd", [1, 0.5, 0.25, 0.125]))
|
|
13
|
-
* // Map { 'c' => 132, 'a' => 545, 'b' => 251, 'd' => 72 }
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* [`weightedRandom()`](https://docs.thi.ng/umbrella/random/functions/weightedRandom.html)
|
|
17
|
-
*
|
|
18
|
-
* @param choices -
|
|
19
|
-
* @param weights -
|
|
20
|
-
*/
|
|
21
|
-
export const choices = (choices, weights, rnd = SYSTEM) => repeatedly(weights
|
|
22
|
-
? weightedRandom(ensureArray(choices), weights, rnd)
|
|
23
|
-
: () => choices[rnd.float(choices.length) | 0]);
|
|
5
|
+
const choices = (choices2, weights, rnd = SYSTEM) => repeatedly(
|
|
6
|
+
weights ? weightedRandom(ensureArray(choices2), weights, rnd) : () => choices2[rnd.float(choices2.length) | 0]
|
|
7
|
+
);
|
|
8
|
+
export {
|
|
9
|
+
choices
|
|
10
|
+
};
|
package/comp.js
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { comp as _comp } from "@thi.ng/compose/comp";
|
|
2
2
|
import { ensureTransducer } from "./ensure.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
function comp(...fns) {
|
|
4
|
+
fns = fns.map(ensureTransducer);
|
|
5
|
+
return _comp.apply(null, fns);
|
|
6
6
|
}
|
|
7
|
+
export {
|
|
8
|
+
comp
|
|
9
|
+
};
|
package/compr.js
CHANGED
|
@@ -1,25 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
* @remarks
|
|
7
|
-
* `rfn[2]` reduces values of type `B` into an accumulator of type `A`.
|
|
8
|
-
* `fn` accepts values of type `C` and produces interim results of type
|
|
9
|
-
* `B`, which are then (possibly) passed to the "inner" `rfn[2]`
|
|
10
|
-
* function. Therefore the resulting reducer takes inputs of `C` and an
|
|
11
|
-
* accumulator of type `A`.
|
|
12
|
-
*
|
|
13
|
-
* It is assumed that `fn` internally calls `rfn[2]` to pass its own
|
|
14
|
-
* results for further processing by the nested reducer `rfn`.
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* compR(rfn, fn)
|
|
19
|
-
* // [rfn[0], rfn[1], fn]
|
|
20
|
-
* ```
|
|
21
|
-
*
|
|
22
|
-
* @param rfn -
|
|
23
|
-
* @param fn -
|
|
24
|
-
*/
|
|
25
|
-
export const compR = (rfn, fn) => [rfn[0], rfn[1], fn];
|
|
1
|
+
const compR = (rfn, fn) => [rfn[0], rfn[1], fn];
|
|
2
|
+
export {
|
|
3
|
+
compR
|
|
4
|
+
};
|
package/concat.js
CHANGED
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
import { ensureIterable } from "@thi.ng/arrays/ensure-iterable";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
*
|
|
7
|
-
* @example
|
|
8
|
-
* ```ts
|
|
9
|
-
* [...concat([1, 2, 3], null, [4, 5])]
|
|
10
|
-
* // [ 1, 2, 3, 4, 5 ]
|
|
11
|
-
*
|
|
12
|
-
* [...concat([1, 2, 3, undefined], null, [4, 5])]
|
|
13
|
-
* // [ 1, 2, 3, undefined, 4, 5 ]
|
|
14
|
-
* ```
|
|
15
|
-
*
|
|
16
|
-
* @param xs -
|
|
17
|
-
*/
|
|
18
|
-
export function* concat(...xs) {
|
|
19
|
-
for (let x of xs) {
|
|
20
|
-
x != null && (yield* ensureIterable(x));
|
|
21
|
-
}
|
|
2
|
+
function* concat(...xs) {
|
|
3
|
+
for (let x of xs) {
|
|
4
|
+
x != null && (yield* ensureIterable(x));
|
|
5
|
+
}
|
|
22
6
|
}
|
|
7
|
+
export {
|
|
8
|
+
concat
|
|
9
|
+
};
|
package/conj.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { reduce, reducer } from "./reduce.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
function conj(xs) {
|
|
3
|
+
return xs ? reduce(conj(), xs) : reducer(
|
|
4
|
+
() => /* @__PURE__ */ new Set(),
|
|
5
|
+
(acc, x) => acc.add(x)
|
|
6
|
+
);
|
|
6
7
|
}
|
|
8
|
+
export {
|
|
9
|
+
conj
|
|
10
|
+
};
|
package/consume.js
CHANGED
|
@@ -1,25 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* should be applied.
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* consume(repeatedly2d((x, y) => console.log("output:", [x, y]), 2, 3));
|
|
12
|
-
* // output: [ 0, 0 ]
|
|
13
|
-
* // output: [ 1, 0 ]
|
|
14
|
-
* // output: [ 0, 1 ]
|
|
15
|
-
* // output: [ 1, 1 ]
|
|
16
|
-
* // output: [ 0, 2 ]
|
|
17
|
-
* // output: [ 1, 2 ]
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @param src
|
|
21
|
-
*/
|
|
22
|
-
export const consume = (src) => {
|
|
23
|
-
for (let _ of src)
|
|
24
|
-
;
|
|
1
|
+
const consume = (src) => {
|
|
2
|
+
for (let _ of src)
|
|
3
|
+
;
|
|
4
|
+
};
|
|
5
|
+
export {
|
|
6
|
+
consume
|
|
25
7
|
};
|
package/converge.js
CHANGED
|
@@ -2,20 +2,22 @@ import { SEMAPHORE } from "@thi.ng/api/api";
|
|
|
2
2
|
import { compR } from "./compr.js";
|
|
3
3
|
import { __iter } from "./iterator.js";
|
|
4
4
|
import { ensureReduced } from "./reduced.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
}));
|
|
5
|
+
function converge(...args) {
|
|
6
|
+
return __iter(converge, args) || ((rfn) => {
|
|
7
|
+
const r = rfn[2];
|
|
8
|
+
const pred = args[0];
|
|
9
|
+
let prev = SEMAPHORE;
|
|
10
|
+
let done = false;
|
|
11
|
+
return compR(rfn, (acc, x) => {
|
|
12
|
+
if (done || prev !== SEMAPHORE && pred(prev, x)) {
|
|
13
|
+
done = true;
|
|
14
|
+
return ensureReduced(r(acc, x));
|
|
15
|
+
}
|
|
16
|
+
prev = x;
|
|
17
|
+
return r(acc, x);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
21
20
|
}
|
|
21
|
+
export {
|
|
22
|
+
converge
|
|
23
|
+
};
|
package/convolve.js
CHANGED
|
@@ -6,62 +6,74 @@ import { range } from "./range.js";
|
|
|
6
6
|
import { range2d } from "./range2d.js";
|
|
7
7
|
import { transduce } from "./transduce.js";
|
|
8
8
|
import { zip } from "./zip.js";
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
const buildKernel1d = (weights, w) => {
|
|
10
|
+
const w2 = w >> 1;
|
|
11
|
+
return [...zip(weights, range(-w2, w2 + 1))];
|
|
12
12
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
13
|
+
const buildKernel2d = (weights, w, h = w) => {
|
|
14
|
+
const w2 = w >> 1;
|
|
15
|
+
const h2 = h >> 1;
|
|
16
|
+
return [...zip(weights, range2d(-w2, w2 + 1, -h2, h2 + 1))];
|
|
17
|
+
};
|
|
18
|
+
const kernelLookup1d = (src, x, width, wrap, border) => wrap ? ({ 0: w, 1: ox }) => {
|
|
19
|
+
const xx = x < -ox ? width + ox : x >= width - ox ? ox - 1 : x + ox;
|
|
20
|
+
return w * src[xx];
|
|
21
|
+
} : ({ 0: w, 1: ox }) => {
|
|
22
|
+
return x < -ox || x >= width - ox ? border : w * src[x + ox];
|
|
23
|
+
};
|
|
24
|
+
const kernelLookup2d = (src, x, y, width, height, wrap, border) => wrap ? ({ 0: w, 1: { 0: ox, 1: oy } }) => {
|
|
25
|
+
const xx = x < -ox ? width + ox : x >= width - ox ? ox - 1 : x + ox;
|
|
26
|
+
const yy = y < -oy ? height + oy : y >= height - oy ? oy - 1 : y + oy;
|
|
27
|
+
return w * src[yy * width + xx];
|
|
28
|
+
} : ({ 0: w, 1: { 0: ox, 1: oy } }) => {
|
|
29
|
+
return x < -ox || y < -oy || x >= width - ox || y >= height - oy ? border : w * src[(y + oy) * width + x + ox];
|
|
17
30
|
};
|
|
18
|
-
const kernelLookup1d = (src, x, width, wrap, border) => wrap
|
|
19
|
-
? ({ 0: w, 1: ox }) => {
|
|
20
|
-
const xx = x < -ox ? width + ox : x >= width - ox ? ox - 1 : x + ox;
|
|
21
|
-
return w * src[xx];
|
|
22
|
-
}
|
|
23
|
-
: ({ 0: w, 1: ox }) => {
|
|
24
|
-
return x < -ox || x >= width - ox ? border : w * src[x + ox];
|
|
25
|
-
};
|
|
26
|
-
const kernelLookup2d = (src, x, y, width, height, wrap, border) => wrap
|
|
27
|
-
? ({ 0: w, 1: { 0: ox, 1: oy } }) => {
|
|
28
|
-
const xx = x < -ox ? width + ox : x >= width - ox ? ox - 1 : x + ox;
|
|
29
|
-
const yy = y < -oy ? height + oy : y >= height - oy ? oy - 1 : y + oy;
|
|
30
|
-
return w * src[yy * width + xx];
|
|
31
|
-
}
|
|
32
|
-
: ({ 0: w, 1: { 0: ox, 1: oy } }) => {
|
|
33
|
-
return x < -ox || y < -oy || x >= width - ox || y >= height - oy
|
|
34
|
-
? border
|
|
35
|
-
: w * src[(y + oy) * width + x + ox];
|
|
36
|
-
};
|
|
37
31
|
const kernelError = () => illegalArgs(`no kernel or kernel config`);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
32
|
+
function convolve1d(opts, indices) {
|
|
33
|
+
if (indices) {
|
|
34
|
+
return iterator1(convolve1d(opts), indices);
|
|
35
|
+
}
|
|
36
|
+
const { src, width } = opts;
|
|
37
|
+
const wrap = opts.wrap !== false;
|
|
38
|
+
const border = opts.border || 0;
|
|
39
|
+
const rfn = opts.reduce || add;
|
|
40
|
+
let kernel = opts.kernel;
|
|
41
|
+
if (!kernel) {
|
|
42
|
+
!(opts.weights && opts.kwidth) && kernelError();
|
|
43
|
+
kernel = buildKernel1d(opts.weights, opts.kwidth);
|
|
44
|
+
}
|
|
45
|
+
return map(
|
|
46
|
+
(p) => transduce(
|
|
47
|
+
map(kernelLookup1d(src, p, width, wrap, border)),
|
|
48
|
+
rfn(),
|
|
49
|
+
kernel
|
|
50
|
+
)
|
|
51
|
+
);
|
|
52
52
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
53
|
+
function convolve2d(opts, indices) {
|
|
54
|
+
if (indices) {
|
|
55
|
+
return iterator1(convolve2d(opts), indices);
|
|
56
|
+
}
|
|
57
|
+
const { src, width, height } = opts;
|
|
58
|
+
const wrap = opts.wrap !== false;
|
|
59
|
+
const border = opts.border || 0;
|
|
60
|
+
const rfn = opts.reduce || add;
|
|
61
|
+
let kernel = opts.kernel;
|
|
62
|
+
if (!kernel) {
|
|
63
|
+
!(opts.weights && opts.kwidth && opts.kheight) && kernelError();
|
|
64
|
+
kernel = buildKernel2d(opts.weights, opts.kwidth, opts.kheight);
|
|
65
|
+
}
|
|
66
|
+
return map(
|
|
67
|
+
(p) => transduce(
|
|
68
|
+
map(kernelLookup2d(src, p[0], p[1], width, height, wrap, border)),
|
|
69
|
+
rfn(),
|
|
70
|
+
kernel
|
|
71
|
+
)
|
|
72
|
+
);
|
|
67
73
|
}
|
|
74
|
+
export {
|
|
75
|
+
buildKernel1d,
|
|
76
|
+
buildKernel2d,
|
|
77
|
+
convolve1d,
|
|
78
|
+
convolve2d
|
|
79
|
+
};
|
package/count.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { $$reduce, reducer } from "./reduce.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
function count(...args) {
|
|
3
|
+
const res = $$reduce(count, args);
|
|
4
|
+
if (res !== void 0) {
|
|
5
|
+
return res;
|
|
6
|
+
}
|
|
7
|
+
let offset = args[0] || 0;
|
|
8
|
+
let step = args[1] || 1;
|
|
9
|
+
return reducer(
|
|
10
|
+
() => offset,
|
|
11
|
+
(acc, _) => acc + step
|
|
12
|
+
);
|
|
10
13
|
}
|
|
14
|
+
export {
|
|
15
|
+
count
|
|
16
|
+
};
|
package/curve.js
CHANGED
|
@@ -1,46 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
*
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
* The curvature can be controlled via the logarithmic `rate` param. Recommended
|
|
11
|
-
* range [0.0001 - 10000] (curved -> linear). Default: 0.1
|
|
12
|
-
*
|
|
13
|
-
* Similar functionality (w/ more options) is availble here:
|
|
14
|
-
* [`curve()`](https://docs.thi.ng/umbrella/dsp/functions/curve.html).
|
|
15
|
-
*
|
|
16
|
-
* @example
|
|
17
|
-
* ```ts
|
|
18
|
-
* [...curve(50, 100, 10, 2)]
|
|
19
|
-
* // [
|
|
20
|
-
* // 50,
|
|
21
|
-
* // 73.193,
|
|
22
|
-
* // 85.649,
|
|
23
|
-
* // 92.339,
|
|
24
|
-
* // 95.932,
|
|
25
|
-
* // 97.861,
|
|
26
|
-
* // 98.897,
|
|
27
|
-
* // 99.454,
|
|
28
|
-
* // 99.753,
|
|
29
|
-
* // 99.913,
|
|
30
|
-
* // 100
|
|
31
|
-
* // ]
|
|
32
|
-
* ```
|
|
33
|
-
*
|
|
34
|
-
* @param start -
|
|
35
|
-
* @param end -
|
|
36
|
-
* @param steps -
|
|
37
|
-
* @param falloff -
|
|
38
|
-
*/
|
|
39
|
-
export function* curve(start, end, steps = 10, rate = 0.1) {
|
|
40
|
-
const c = Math.exp(-Math.log((Math.abs(end - start) + rate) / rate) / steps);
|
|
41
|
-
const offset = (start < end ? end + rate : end - rate) * (1 - c);
|
|
42
|
-
steps > 0 && (yield start);
|
|
43
|
-
for (let x = start; steps-- > 0;) {
|
|
44
|
-
yield (x = offset + x * c);
|
|
45
|
-
}
|
|
1
|
+
function* curve(start, end, steps = 10, rate = 0.1) {
|
|
2
|
+
const c = Math.exp(
|
|
3
|
+
-Math.log((Math.abs(end - start) + rate) / rate) / steps
|
|
4
|
+
);
|
|
5
|
+
const offset = (start < end ? end + rate : end - rate) * (1 - c);
|
|
6
|
+
steps > 0 && (yield start);
|
|
7
|
+
for (let x = start; steps-- > 0; ) {
|
|
8
|
+
yield x = offset + x * c;
|
|
9
|
+
}
|
|
46
10
|
}
|
|
11
|
+
export {
|
|
12
|
+
curve
|
|
13
|
+
};
|
package/cycle.js
CHANGED
|
@@ -1,36 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* [...take(5, cycle([1, 2, 3]))]
|
|
13
|
-
* // [1, 2, 3, 1, 2]
|
|
14
|
-
*
|
|
15
|
-
* // only produce 2 cycles
|
|
16
|
-
* [...cycle(range(3), 2)]
|
|
17
|
-
* // [ 0, 1, 2, 0, 1, 2 ]
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* @param input -
|
|
21
|
-
* @param num -
|
|
22
|
-
*/
|
|
23
|
-
export function* cycle(input, num = Infinity) {
|
|
24
|
-
if (num < 1)
|
|
25
|
-
return;
|
|
26
|
-
let cache = [];
|
|
27
|
-
for (let i of input) {
|
|
28
|
-
cache.push(i);
|
|
29
|
-
yield i;
|
|
30
|
-
}
|
|
31
|
-
if (cache.length > 0) {
|
|
32
|
-
while (--num > 0) {
|
|
33
|
-
yield* cache;
|
|
34
|
-
}
|
|
1
|
+
function* cycle(input, num = Infinity) {
|
|
2
|
+
if (num < 1)
|
|
3
|
+
return;
|
|
4
|
+
let cache = [];
|
|
5
|
+
for (let i of input) {
|
|
6
|
+
cache.push(i);
|
|
7
|
+
yield i;
|
|
8
|
+
}
|
|
9
|
+
if (cache.length > 0) {
|
|
10
|
+
while (--num > 0) {
|
|
11
|
+
yield* cache;
|
|
35
12
|
}
|
|
13
|
+
}
|
|
36
14
|
}
|
|
15
|
+
export {
|
|
16
|
+
cycle
|
|
17
|
+
};
|