@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.
Files changed (158) hide show
  1. package/CHANGELOG.md +1 -1
  2. package/add.js +5 -2
  3. package/api.js +0 -1
  4. package/as-iterable.js +5 -8
  5. package/assoc-map.js +8 -4
  6. package/assoc-obj.js +8 -4
  7. package/auto-obj.js +9 -5
  8. package/benchmark.js +14 -13
  9. package/cat.js +15 -49
  10. package/choices.js +6 -19
  11. package/comp.js +6 -3
  12. package/compr.js +4 -25
  13. package/concat.js +7 -20
  14. package/conj.js +8 -4
  15. package/consume.js +6 -24
  16. package/converge.js +18 -16
  17. package/convolve.js +66 -54
  18. package/count.js +14 -8
  19. package/curve.js +12 -45
  20. package/cycle.js +15 -34
  21. package/dedupe.js +21 -21
  22. package/deep-transform.js +17 -73
  23. package/delayed.js +4 -10
  24. package/distinct.js +17 -16
  25. package/div.js +8 -4
  26. package/drop-nth.js +12 -9
  27. package/drop-while.js +13 -8
  28. package/drop.js +12 -8
  29. package/dup.js +5 -6
  30. package/duplicate.js +13 -12
  31. package/ensure.js +4 -1
  32. package/every.js +13 -9
  33. package/extend-sides.js +15 -40
  34. package/fill.js +16 -9
  35. package/filter-fuzzy.js +13 -8
  36. package/filter.js +8 -7
  37. package/flatten-with.js +21 -20
  38. package/flatten.js +8 -2
  39. package/flatten1.js +5 -2
  40. package/frequencies.js +5 -3
  41. package/group-binary.js +15 -84
  42. package/group-by-map.js +25 -21
  43. package/group-by-obj.js +23 -22
  44. package/index.js +0 -4
  45. package/indexed.js +10 -7
  46. package/interleave.js +12 -11
  47. package/internal/drain.js +8 -15
  48. package/internal/group-opts.js +7 -11
  49. package/internal/mathop.js +10 -17
  50. package/internal/sort-opts.js +7 -11
  51. package/interpolate-hermite.js +10 -2
  52. package/interpolate-linear.js +10 -2
  53. package/interpolate.js +8 -4
  54. package/interpose.js +17 -16
  55. package/iterate.js +8 -28
  56. package/iterator.js +36 -62
  57. package/juxtr.js +74 -71
  58. package/keep.js +12 -7
  59. package/key-permutations.js +7 -30
  60. package/key-selector.js +4 -1
  61. package/keys.js +8 -16
  62. package/labeled.js +5 -4
  63. package/last.js +5 -2
  64. package/length.js +7 -6
  65. package/line.js +6 -29
  66. package/lookup.js +10 -44
  67. package/map-deep.js +5 -4
  68. package/map-indexed.js +10 -8
  69. package/map-keys.js +16 -13
  70. package/map-nth.js +34 -33
  71. package/map-vals.js +16 -13
  72. package/map.js +11 -13
  73. package/mapcat-indexed.js +5 -3
  74. package/mapcat.js +5 -2
  75. package/match-first.js +5 -4
  76. package/match-last.js +5 -4
  77. package/max-compare.js +11 -8
  78. package/max-mag.js +8 -4
  79. package/max.js +8 -4
  80. package/mean.js +10 -9
  81. package/min-compare.js +11 -8
  82. package/min-mag.js +8 -4
  83. package/min-max.js +4 -5
  84. package/min.js +8 -4
  85. package/moving-average.js +17 -16
  86. package/moving-median.js +17 -9
  87. package/mul.js +5 -2
  88. package/multiplex-obj.js +14 -8
  89. package/multiplex.js +12 -2
  90. package/noop.js +4 -6
  91. package/norm-count.js +10 -7
  92. package/norm-frequencies-auto.js +21 -18
  93. package/norm-frequencies.js +8 -6
  94. package/norm-range.js +20 -45
  95. package/package.json +14 -12
  96. package/pad-last.js +20 -19
  97. package/pad-sides.js +4 -35
  98. package/page.js +5 -3
  99. package/pairs.js +8 -22
  100. package/palindrome.js +5 -6
  101. package/partition-by.js +34 -34
  102. package/partition-of.js +14 -13
  103. package/partition-sort.js +15 -7
  104. package/partition-sync.js +118 -119
  105. package/partition-time.js +12 -11
  106. package/partition-when.js +28 -27
  107. package/partition.js +45 -44
  108. package/peek.js +5 -2
  109. package/permutations.js +35 -58
  110. package/pluck.js +5 -4
  111. package/push-copy.js +7 -1
  112. package/push-sort.js +9 -8
  113. package/push.js +8 -4
  114. package/range-nd.js +7 -36
  115. package/range.js +47 -51
  116. package/range2d.js +25 -22
  117. package/range3d.js +30 -27
  118. package/rechunk.js +34 -31
  119. package/reduce.js +53 -63
  120. package/reduced.js +19 -12
  121. package/reductions.js +18 -17
  122. package/rename.js +27 -16
  123. package/renamer.js +46 -52
  124. package/repeat.js +7 -19
  125. package/repeatedly.js +7 -22
  126. package/repeatedly2d.js +8 -24
  127. package/repeatedly3d.js +10 -26
  128. package/reverse.js +9 -18
  129. package/run.js +10 -8
  130. package/sample.js +17 -11
  131. package/scan.js +26 -24
  132. package/select-keys.js +5 -4
  133. package/side-effect.js +4 -8
  134. package/sliding-window.js +22 -19
  135. package/some.js +13 -9
  136. package/sorted-keys.js +5 -11
  137. package/step.js +16 -66
  138. package/str.js +10 -8
  139. package/stream-shuffle.js +41 -40
  140. package/stream-sort.js +25 -22
  141. package/struct.js +16 -4
  142. package/sub.js +5 -2
  143. package/swizzle.js +5 -4
  144. package/symmetric.js +13 -23
  145. package/take-last.js +18 -17
  146. package/take-nth.js +12 -9
  147. package/take-while.js +13 -8
  148. package/take.js +12 -12
  149. package/throttle-time.js +11 -10
  150. package/throttle.js +9 -8
  151. package/toggle.js +12 -11
  152. package/trace.js +4 -1
  153. package/transduce.js +23 -19
  154. package/tween.js +33 -94
  155. package/vals.js +8 -16
  156. package/word-wrap.js +25 -22
  157. package/wrap-sides.js +15 -31
  158. 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
- export 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];
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
- * Iterator yielding the Cartesian Product for `n` items of `m` values
29
- * each.
30
- *
31
- * @remarks
32
- * If `m` is not given, defaults to value of `n`. The range of `m` is
33
- * `0..m-1`. The optional `offsets` array can be used to define start
34
- * values for each dimension.
35
- *
36
- * @example
37
- * ```ts
38
- * [...permutationsN(2)]
39
- * // [ [0, 0], [0, 1], [1, 0], [1, 1] ]
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
- export function pluck(key, src) {
5
- return isIterable(src)
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
- export const pushCopy = () => reducer(() => [], (acc, x) => ((acc = acc.slice()).push(x), acc));
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
- export function pushSort(cmp = compare, xs) {
3
- return xs
4
- ? [...xs].sort(cmp)
5
- : [
6
- () => [],
7
- (acc) => acc.sort(cmp),
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
- export function push(xs) {
3
- return xs
4
- ? [...xs]
5
- : reducer(() => [], (acc, x) => (acc.push(x), acc));
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
- * If called with one vector, yields an iterator for the n-dimensional
7
- * interval: `[[0, 0,...] .. [x, y,...])`. If called with two vectors,
8
- * the first vector defines the inclusive interval start and the 2nd
9
- * vector the exclusive interval end. Each dimension can also contain
10
- * negative values.
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
- export function range(from, to, step) {
3
- return new Range(from, to, step);
2
+ function range(from, to, step) {
3
+ return new Range(from, to, step);
4
4
  }
5
- /**
6
- * Simple class wrapper around given range interval and implementing `Iterable`
7
- * and {@link IReducible} interfaces, the latter is used to accelerate use with
8
- * {@link reduce}.
9
- */
10
- export class Range {
11
- from;
12
- to;
13
- step;
14
- constructor(from, to, step) {
15
- if (from === undefined) {
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
- *[Symbol.iterator]() {
29
- let { from, to, step } = this;
30
- if (step > 0) {
31
- while (from < to) {
32
- yield from;
33
- from += step;
34
- }
35
- }
36
- else if (step < 0) {
37
- while (from > to) {
38
- yield from;
39
- from += step;
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
- $reduce(rfn, acc) {
44
- const step = this.step;
45
- if (step > 0) {
46
- for (let i = this.from, n = this.to; i < n && !isReduced(acc); i += step) {
47
- acc = rfn(acc, i);
48
- }
49
- }
50
- else {
51
- for (let i = this.from, n = this.to; i > n && !isReduced(acc); i += step) {
52
- acc = rfn(acc, i);
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
- export 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];
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
- export 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
- }
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
- export 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
- };
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
- ? [undefined, args[1]]
8
- : args.length === 3
9
- ? [args[1], args[2]]
10
- : illegalArity(args.length);
11
- export function reduce(...args) {
12
- const rfn = args[0];
13
- const init = rfn[0];
14
- const complete = rfn[1];
15
- const reduce = rfn[2];
16
- args = parseArgs(args);
17
- const acc = args[0] == null ? init() : args[0];
18
- const xs = args[1];
19
- return unreduced(complete(implementsFunction(xs, "$reduce")
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
- export function reduceRight(...args) {
26
- const rfn = args[0];
27
- const init = rfn[0];
28
- const complete = rfn[1];
29
- const reduce = rfn[2];
30
- args = parseArgs(args);
31
- let acc = args[0] == null ? init() : args[0];
32
- const xs = args[1];
33
- for (let i = xs.length; i-- > 0;) {
34
- acc = reduce(acc, xs[i]);
35
- if (isReduced(acc)) {
36
- acc = acc.deref();
37
- break;
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
- return unreduced(complete(acc));
35
+ }
36
+ return unreduced(complete(acc));
41
37
  }
42
38
  const reduceArray = (rfn, acc, xs) => {
43
- for (let i = 0, n = xs.length; i < n; i++) {
44
- acc = rfn(acc, xs[i]);
45
- if (isReduced(acc)) {
46
- acc = acc.deref();
47
- break;
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
- return acc;
45
+ }
46
+ return acc;
51
47
  };
52
48
  const reduceIterable = (rfn, acc, xs) => {
53
- for (let x of xs) {
54
- acc = rfn(acc, x);
55
- if (isReduced(acc)) {
56
- acc = acc.deref();
57
- break;
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
- return acc;
55
+ }
56
+ return acc;
61
57
  };
62
- /**
63
- * Convenience helper for building a full {@link Reducer} using the identity
64
- * function (i.e. `(x) => x`) as completion step (true for 90% of all
65
- * bundled transducers).
66
- *
67
- * @param init - init step of reducer
68
- * @param rfn - reduction step of reducer
69
- */
70
- export const reducer = (init, rfn) => [init, (acc) => acc, rfn];
71
- export const $$reduce = (rfn, args) => {
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
- export class Reduced {
2
- value;
3
- constructor(val) {
4
- this.value = val;
5
- }
6
- deref() {
7
- return this.value;
8
- }
1
+ class Reduced {
2
+ value;
3
+ constructor(val) {
4
+ this.value = val;
5
+ }
6
+ deref() {
7
+ return this.value;
8
+ }
9
9
  }
10
- export const reduced = (x) => new Reduced(x);
11
- export const isReduced = (x) => x instanceof Reduced;
12
- export const ensureReduced = (x) => x instanceof Reduced ? x : new Reduced(x);
13
- export const unreduced = (x) => (x instanceof Reduced ? x.deref() : x);
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
+ };