@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/permutations.js
CHANGED
|
@@ -1,64 +1,41 @@
|
|
|
1
1
|
import { ensureArrayLike } from "@thi.ng/arrays/ensure-array";
|
|
2
2
|
import { illegalArgs } from "@thi.ng/errors/illegal-arguments";
|
|
3
3
|
import { range } from "./range.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
23
|
-
step[n]++;
|
|
24
|
-
yield tuple;
|
|
4
|
+
function* permutations(...src) {
|
|
5
|
+
const n = src.length - 1;
|
|
6
|
+
if (n < 0) {
|
|
7
|
+
return;
|
|
8
|
+
}
|
|
9
|
+
const step = new Array(n + 1).fill(0);
|
|
10
|
+
const realized = src.map(ensureArrayLike);
|
|
11
|
+
const total = realized.reduce((acc, x) => acc * x.length, 1);
|
|
12
|
+
for (let i = 0; i < total; i++) {
|
|
13
|
+
const tuple = [];
|
|
14
|
+
for (let j = n; j >= 0; j--) {
|
|
15
|
+
const r = realized[j];
|
|
16
|
+
let s = step[j];
|
|
17
|
+
if (s === r.length) {
|
|
18
|
+
step[j] = s = 0;
|
|
19
|
+
j > 0 && step[j - 1]++;
|
|
20
|
+
}
|
|
21
|
+
tuple[j] = r[s];
|
|
25
22
|
}
|
|
23
|
+
step[n]++;
|
|
24
|
+
yield tuple;
|
|
25
|
+
}
|
|
26
26
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
* [...permutationsN(2, 3)]
|
|
42
|
-
* // [ [0, 0], [0, 1], [0, 2],
|
|
43
|
-
* // [1, 0], [1, 1], [1, 2],
|
|
44
|
-
* // [2, 0], [2, 1], [2, 2] ]
|
|
45
|
-
*
|
|
46
|
-
* [...permutationsN(2, 2, [10, 20])]
|
|
47
|
-
* // [ [ 10, 20 ], [ 10, 21 ], [ 11, 20 ], [ 11, 21 ] ]
|
|
48
|
-
* ```
|
|
49
|
-
*
|
|
50
|
-
* @param n -
|
|
51
|
-
* @param m -
|
|
52
|
-
* @param offsets -
|
|
53
|
-
*/
|
|
54
|
-
export const permutationsN = (n, m = n, offsets) => {
|
|
55
|
-
if (offsets && offsets.length < n) {
|
|
56
|
-
illegalArgs(`insufficient offsets, got ${offsets.length}, needed ${n}`);
|
|
57
|
-
}
|
|
58
|
-
const seqs = [];
|
|
59
|
-
while (n-- > 0) {
|
|
60
|
-
const o = offsets ? offsets[n] : 0;
|
|
61
|
-
seqs[n] = range(o, o + m);
|
|
62
|
-
}
|
|
63
|
-
return permutations.apply(null, seqs);
|
|
27
|
+
const permutationsN = (n, m = n, offsets) => {
|
|
28
|
+
if (offsets && offsets.length < n) {
|
|
29
|
+
illegalArgs(`insufficient offsets, got ${offsets.length}, needed ${n}`);
|
|
30
|
+
}
|
|
31
|
+
const seqs = [];
|
|
32
|
+
while (n-- > 0) {
|
|
33
|
+
const o = offsets ? offsets[n] : 0;
|
|
34
|
+
seqs[n] = range(o, o + m);
|
|
35
|
+
}
|
|
36
|
+
return permutations.apply(null, seqs);
|
|
37
|
+
};
|
|
38
|
+
export {
|
|
39
|
+
permutations,
|
|
40
|
+
permutationsN
|
|
64
41
|
};
|
package/pluck.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { isIterable } from "@thi.ng/checks/is-iterable";
|
|
2
2
|
import { iterator1 } from "./iterator.js";
|
|
3
3
|
import { map } from "./map.js";
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
? iterator1(pluck(key), src)
|
|
7
|
-
: map((x) => x[key]);
|
|
4
|
+
function pluck(key, src) {
|
|
5
|
+
return isIterable(src) ? iterator1(pluck(key), src) : map((x) => x[key]);
|
|
8
6
|
}
|
|
7
|
+
export {
|
|
8
|
+
pluck
|
|
9
|
+
};
|
package/push-copy.js
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import { reducer } from "./reduce.js";
|
|
2
|
-
|
|
2
|
+
const pushCopy = () => reducer(
|
|
3
|
+
() => [],
|
|
4
|
+
(acc, x) => ((acc = acc.slice()).push(x), acc)
|
|
5
|
+
);
|
|
6
|
+
export {
|
|
7
|
+
pushCopy
|
|
8
|
+
};
|
package/push-sort.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { compare } from "@thi.ng/compare/compare";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
(acc, x) => (acc.push(x), acc),
|
|
9
|
-
];
|
|
2
|
+
function pushSort(cmp = compare, xs) {
|
|
3
|
+
return xs ? [...xs].sort(cmp) : [
|
|
4
|
+
() => [],
|
|
5
|
+
(acc) => acc.sort(cmp),
|
|
6
|
+
(acc, x) => (acc.push(x), acc)
|
|
7
|
+
];
|
|
10
8
|
}
|
|
9
|
+
export {
|
|
10
|
+
pushSort
|
|
11
|
+
};
|
package/push.js
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { reducer } from "./reduce.js";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
2
|
+
function push(xs) {
|
|
3
|
+
return xs ? [...xs] : reducer(
|
|
4
|
+
() => [],
|
|
5
|
+
(acc, x) => (acc.push(x), acc)
|
|
6
|
+
);
|
|
6
7
|
}
|
|
8
|
+
export {
|
|
9
|
+
push
|
|
10
|
+
};
|
package/range-nd.js
CHANGED
|
@@ -2,39 +2,10 @@ import { map } from "./map.js";
|
|
|
2
2
|
import { permutations } from "./permutations.js";
|
|
3
3
|
import { range, Range } from "./range.js";
|
|
4
4
|
import { zip } from "./zip.js";
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* @example
|
|
13
|
-
* ```ts
|
|
14
|
-
* [...rangeNd([2])]
|
|
15
|
-
* // [ [ 0 ], [ 1 ] ]
|
|
16
|
-
*
|
|
17
|
-
* [...rangeNd([2, -2])]
|
|
18
|
-
* // [ [ 0, 0 ], [ 0, -1 ], [ 1, 0 ], [ 1, -1 ] ]
|
|
19
|
-
*
|
|
20
|
-
* [...rangeNd([-1,2], [1,3])]
|
|
21
|
-
* // [ [ -1, 2 ], [ -1, 3 ], [ 0, 2 ], [ 0, 3 ] ]
|
|
22
|
-
*
|
|
23
|
-
* [...rangeNd([2, 2, 2])]
|
|
24
|
-
* // [
|
|
25
|
-
* // [ 0, 0, 0 ],
|
|
26
|
-
* // [ 0, 0, 1 ],
|
|
27
|
-
* // [ 0, 1, 0 ],
|
|
28
|
-
* // [ 0, 1, 1 ],
|
|
29
|
-
* // [ 1, 0, 0 ],
|
|
30
|
-
* // [ 1, 0, 1 ],
|
|
31
|
-
* // [ 1, 1, 0 ],
|
|
32
|
-
* // [ 1, 1, 1 ]
|
|
33
|
-
* // ]
|
|
34
|
-
* ```
|
|
35
|
-
*
|
|
36
|
-
* @param vec -
|
|
37
|
-
*/
|
|
38
|
-
export const rangeNd = (min, max) => permutations.apply(null, ((max
|
|
39
|
-
? [...map(([a, b]) => range(a, b), zip(min, max))]
|
|
40
|
-
: [...map(range, min)])));
|
|
5
|
+
const rangeNd = (min, max) => permutations.apply(
|
|
6
|
+
null,
|
|
7
|
+
max ? [...map(([a, b]) => range(a, b), zip(min, max))] : [...map(range, min)]
|
|
8
|
+
);
|
|
9
|
+
export {
|
|
10
|
+
rangeNd
|
|
11
|
+
};
|
package/range.js
CHANGED
|
@@ -1,57 +1,53 @@
|
|
|
1
1
|
import { isReduced, Reduced } from "./reduced.js";
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
function range(from, to, step) {
|
|
3
|
+
return new Range(from, to, step);
|
|
4
4
|
}
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
from = 0;
|
|
17
|
-
to = Infinity;
|
|
18
|
-
}
|
|
19
|
-
else if (to === undefined) {
|
|
20
|
-
to = from;
|
|
21
|
-
from = 0;
|
|
22
|
-
}
|
|
23
|
-
step = step === undefined ? (from < to ? 1 : -1) : step;
|
|
24
|
-
this.from = from;
|
|
25
|
-
this.to = to;
|
|
26
|
-
this.step = step;
|
|
5
|
+
class Range {
|
|
6
|
+
from;
|
|
7
|
+
to;
|
|
8
|
+
step;
|
|
9
|
+
constructor(from, to, step) {
|
|
10
|
+
if (from === void 0) {
|
|
11
|
+
from = 0;
|
|
12
|
+
to = Infinity;
|
|
13
|
+
} else if (to === void 0) {
|
|
14
|
+
to = from;
|
|
15
|
+
from = 0;
|
|
27
16
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
17
|
+
step = step === void 0 ? from < to ? 1 : -1 : step;
|
|
18
|
+
this.from = from;
|
|
19
|
+
this.to = to;
|
|
20
|
+
this.step = step;
|
|
21
|
+
}
|
|
22
|
+
*[Symbol.iterator]() {
|
|
23
|
+
let { from, to, step } = this;
|
|
24
|
+
if (step > 0) {
|
|
25
|
+
while (from < to) {
|
|
26
|
+
yield from;
|
|
27
|
+
from += step;
|
|
28
|
+
}
|
|
29
|
+
} else if (step < 0) {
|
|
30
|
+
while (from > to) {
|
|
31
|
+
yield from;
|
|
32
|
+
from += step;
|
|
33
|
+
}
|
|
42
34
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
return acc;
|
|
35
|
+
}
|
|
36
|
+
$reduce(rfn, acc) {
|
|
37
|
+
const step = this.step;
|
|
38
|
+
if (step > 0) {
|
|
39
|
+
for (let i = this.from, n = this.to; i < n && !isReduced(acc); i += step) {
|
|
40
|
+
acc = rfn(acc, i);
|
|
41
|
+
}
|
|
42
|
+
} else {
|
|
43
|
+
for (let i = this.from, n = this.to; i > n && !isReduced(acc); i += step) {
|
|
44
|
+
acc = rfn(acc, i);
|
|
45
|
+
}
|
|
56
46
|
}
|
|
47
|
+
return acc;
|
|
48
|
+
}
|
|
57
49
|
}
|
|
50
|
+
export {
|
|
51
|
+
Range,
|
|
52
|
+
range
|
|
53
|
+
};
|
package/range2d.js
CHANGED
|
@@ -1,26 +1,29 @@
|
|
|
1
1
|
import { illegalArity } from "@thi.ng/errors/illegal-arity";
|
|
2
2
|
import { range } from "./range.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
}
|
|
3
|
+
function* range2d(...args) {
|
|
4
|
+
let fromX, toX, stepX;
|
|
5
|
+
let fromY, toY, stepY;
|
|
6
|
+
switch (args.length) {
|
|
7
|
+
case 6:
|
|
8
|
+
stepX = args[4];
|
|
9
|
+
stepY = args[5];
|
|
10
|
+
case 4:
|
|
11
|
+
[fromX, toX, fromY, toY] = args;
|
|
12
|
+
break;
|
|
13
|
+
case 2:
|
|
14
|
+
[toX, toY] = args;
|
|
15
|
+
fromX = fromY = 0;
|
|
16
|
+
break;
|
|
17
|
+
default:
|
|
18
|
+
illegalArity(args.length);
|
|
19
|
+
}
|
|
20
|
+
const rx = range(fromX, toX, stepX);
|
|
21
|
+
for (let y of range(fromY, toY, stepY)) {
|
|
22
|
+
for (let x of rx) {
|
|
23
|
+
yield [x, y];
|
|
25
24
|
}
|
|
25
|
+
}
|
|
26
26
|
}
|
|
27
|
+
export {
|
|
28
|
+
range2d
|
|
29
|
+
};
|
package/range3d.js
CHANGED
|
@@ -1,31 +1,34 @@
|
|
|
1
1
|
import { illegalArity } from "@thi.ng/errors/illegal-arity";
|
|
2
2
|
import { range } from "./range.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
3
|
+
function* range3d(...args) {
|
|
4
|
+
let fromX, toX, stepX;
|
|
5
|
+
let fromY, toY, stepY;
|
|
6
|
+
let fromZ, toZ, stepZ;
|
|
7
|
+
switch (args.length) {
|
|
8
|
+
case 9:
|
|
9
|
+
stepX = args[6];
|
|
10
|
+
stepY = args[7];
|
|
11
|
+
stepZ = args[8];
|
|
12
|
+
case 6:
|
|
13
|
+
[fromX, toX, fromY, toY, fromZ, toZ] = args;
|
|
14
|
+
break;
|
|
15
|
+
case 3:
|
|
16
|
+
[toX, toY, toZ] = args;
|
|
17
|
+
fromX = fromY = fromZ = 0;
|
|
18
|
+
break;
|
|
19
|
+
default:
|
|
20
|
+
illegalArity(args.length);
|
|
21
|
+
}
|
|
22
|
+
const rx = range(fromX, toX, stepX);
|
|
23
|
+
const ry = range(fromY, toY, stepY);
|
|
24
|
+
for (let z of range(fromZ, toZ, stepZ)) {
|
|
25
|
+
for (let y of ry) {
|
|
26
|
+
for (let x of rx) {
|
|
27
|
+
yield [x, y, z];
|
|
28
|
+
}
|
|
30
29
|
}
|
|
30
|
+
}
|
|
31
31
|
}
|
|
32
|
+
export {
|
|
33
|
+
range3d
|
|
34
|
+
};
|
package/rechunk.js
CHANGED
|
@@ -1,34 +1,37 @@
|
|
|
1
1
|
import { iterator, __iter } from "./iterator.js";
|
|
2
2
|
import { isReduced } from "./reduced.js";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
3
|
+
function rechunk(...args) {
|
|
4
|
+
const iter = __iter(rechunk, args, iterator);
|
|
5
|
+
if (iter)
|
|
6
|
+
return iter;
|
|
7
|
+
return ([init, complete, reduce]) => {
|
|
8
|
+
let buf = "";
|
|
9
|
+
const re = args[0] || /\r?\n/;
|
|
10
|
+
return [
|
|
11
|
+
init,
|
|
12
|
+
(acc) => {
|
|
13
|
+
if (buf)
|
|
14
|
+
acc = reduce(acc, buf);
|
|
15
|
+
return complete(acc);
|
|
16
|
+
},
|
|
17
|
+
(acc, chunk) => {
|
|
18
|
+
buf += chunk;
|
|
19
|
+
const res = buf.split(re);
|
|
20
|
+
if (res.length > 1) {
|
|
21
|
+
buf = res.pop();
|
|
22
|
+
for (let l of res) {
|
|
23
|
+
acc = reduce(acc, l);
|
|
24
|
+
if (isReduced(acc)) {
|
|
25
|
+
buf = "";
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return acc;
|
|
31
|
+
}
|
|
32
|
+
];
|
|
33
|
+
};
|
|
34
34
|
}
|
|
35
|
+
export {
|
|
36
|
+
rechunk
|
|
37
|
+
};
|
package/reduce.js
CHANGED
|
@@ -3,76 +3,66 @@ import { isArrayLike } from "@thi.ng/checks/is-arraylike";
|
|
|
3
3
|
import { isIterable } from "@thi.ng/checks/is-iterable";
|
|
4
4
|
import { illegalArity } from "@thi.ng/errors/illegal-arity";
|
|
5
5
|
import { isReduced, unreduced } from "./reduced.js";
|
|
6
|
-
const parseArgs = (args) => args.length === 2
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
? xs.$reduce(reduce, acc)
|
|
21
|
-
: isArrayLike(xs)
|
|
22
|
-
? reduceArray(reduce, acc, xs)
|
|
23
|
-
: reduceIterable(reduce, acc, xs)));
|
|
6
|
+
const parseArgs = (args) => args.length === 2 ? [void 0, args[1]] : args.length === 3 ? [args[1], args[2]] : illegalArity(args.length);
|
|
7
|
+
function reduce(...args) {
|
|
8
|
+
const rfn = args[0];
|
|
9
|
+
const init = rfn[0];
|
|
10
|
+
const complete = rfn[1];
|
|
11
|
+
const reduce2 = rfn[2];
|
|
12
|
+
args = parseArgs(args);
|
|
13
|
+
const acc = args[0] == null ? init() : args[0];
|
|
14
|
+
const xs = args[1];
|
|
15
|
+
return unreduced(
|
|
16
|
+
complete(
|
|
17
|
+
implementsFunction(xs, "$reduce") ? xs.$reduce(reduce2, acc) : isArrayLike(xs) ? reduceArray(reduce2, acc, xs) : reduceIterable(reduce2, acc, xs)
|
|
18
|
+
)
|
|
19
|
+
);
|
|
24
20
|
}
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
21
|
+
function reduceRight(...args) {
|
|
22
|
+
const rfn = args[0];
|
|
23
|
+
const init = rfn[0];
|
|
24
|
+
const complete = rfn[1];
|
|
25
|
+
const reduce2 = rfn[2];
|
|
26
|
+
args = parseArgs(args);
|
|
27
|
+
let acc = args[0] == null ? init() : args[0];
|
|
28
|
+
const xs = args[1];
|
|
29
|
+
for (let i = xs.length; i-- > 0; ) {
|
|
30
|
+
acc = reduce2(acc, xs[i]);
|
|
31
|
+
if (isReduced(acc)) {
|
|
32
|
+
acc = acc.deref();
|
|
33
|
+
break;
|
|
39
34
|
}
|
|
40
|
-
|
|
35
|
+
}
|
|
36
|
+
return unreduced(complete(acc));
|
|
41
37
|
}
|
|
42
38
|
const reduceArray = (rfn, acc, xs) => {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
39
|
+
for (let i = 0, n = xs.length; i < n; i++) {
|
|
40
|
+
acc = rfn(acc, xs[i]);
|
|
41
|
+
if (isReduced(acc)) {
|
|
42
|
+
acc = acc.deref();
|
|
43
|
+
break;
|
|
49
44
|
}
|
|
50
|
-
|
|
45
|
+
}
|
|
46
|
+
return acc;
|
|
51
47
|
};
|
|
52
48
|
const reduceIterable = (rfn, acc, xs) => {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
49
|
+
for (let x of xs) {
|
|
50
|
+
acc = rfn(acc, x);
|
|
51
|
+
if (isReduced(acc)) {
|
|
52
|
+
acc = acc.deref();
|
|
53
|
+
break;
|
|
59
54
|
}
|
|
60
|
-
|
|
55
|
+
}
|
|
56
|
+
return acc;
|
|
61
57
|
};
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
const n = args.length - 1;
|
|
73
|
-
return isIterable(args[n])
|
|
74
|
-
? args.length > 1
|
|
75
|
-
? reduce(rfn.apply(null, args.slice(0, n)), args[n])
|
|
76
|
-
: reduce(rfn(), args[0])
|
|
77
|
-
: undefined;
|
|
58
|
+
const reducer = (init, rfn) => [init, (acc) => acc, rfn];
|
|
59
|
+
const $$reduce = (rfn, args) => {
|
|
60
|
+
const n = args.length - 1;
|
|
61
|
+
return isIterable(args[n]) ? args.length > 1 ? reduce(rfn.apply(null, args.slice(0, n)), args[n]) : reduce(rfn(), args[0]) : void 0;
|
|
62
|
+
};
|
|
63
|
+
export {
|
|
64
|
+
$$reduce,
|
|
65
|
+
reduce,
|
|
66
|
+
reduceRight,
|
|
67
|
+
reducer
|
|
78
68
|
};
|
package/reduced.js
CHANGED
|
@@ -1,13 +1,20 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
class Reduced {
|
|
2
|
+
value;
|
|
3
|
+
constructor(val) {
|
|
4
|
+
this.value = val;
|
|
5
|
+
}
|
|
6
|
+
deref() {
|
|
7
|
+
return this.value;
|
|
8
|
+
}
|
|
9
9
|
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
const reduced = (x) => new Reduced(x);
|
|
11
|
+
const isReduced = (x) => x instanceof Reduced;
|
|
12
|
+
const ensureReduced = (x) => x instanceof Reduced ? x : new Reduced(x);
|
|
13
|
+
const unreduced = (x) => x instanceof Reduced ? x.deref() : x;
|
|
14
|
+
export {
|
|
15
|
+
Reduced,
|
|
16
|
+
ensureReduced,
|
|
17
|
+
isReduced,
|
|
18
|
+
reduced,
|
|
19
|
+
unreduced
|
|
20
|
+
};
|