Skip to content

Commit

Permalink
refactor(transducers): update labeled(), mapIndexed(), partition()
Browse files Browse the repository at this point in the history
- labeled() - add support for label fn instead of just static label
- mapIndexed() - add optional start index arg
- partition() - minor update in completing reducer
  • Loading branch information
postspectacular committed Mar 19, 2018
1 parent bebd118 commit 3bc8d54
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
10 changes: 8 additions & 2 deletions packages/transducers/src/xform/labeled.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { isFunction } from "@thi.ng/checks/is-function";

import { Transducer } from "../api";
import { map } from "./map";

export function labeled<L, T>(id: L): Transducer<T, [L, T]> {
return map((x) => [id, x]);
export function labeled<L, T>(id: L | ((x: T) => L)): Transducer<T, [L, T]> {
return map(
isFunction(id) ?
(x) => [id(x), x] :
(x) => [id, x]
);
}
4 changes: 2 additions & 2 deletions packages/transducers/src/xform/map-indexed.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Reducer, Transducer } from "../api";
import { compR } from "../func/compr";

export function mapIndexed<A, B>(fn: (i: number, x: A) => B): Transducer<A, B> {
export function mapIndexed<A, B>(fn: (i: number, x: A) => B, offset = 0): Transducer<A, B> {
return (rfn: Reducer<any, B>) => {
const r = rfn[2];
let i = 0;
let i = offset;
return compR(rfn,
(acc, x: A) => r(acc, fn(i++, x)));
};
Expand Down
2 changes: 1 addition & 1 deletion packages/transducers/src/xform/partition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function partition<T>(...args: any[]): Transducer<T, T[]> {
return [
init,
(acc) => {
if (buf.length && (all || buf.length === size)) {
if (all && buf.length > 0) {
acc = reduce(acc, buf);
buf = [];
}
Expand Down

0 comments on commit 3bc8d54

Please sign in to comment.