Skip to content

Commit

Permalink
fix(transducers): use child reducer completion step in groupByMap/Obj()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Dec 26, 2019
1 parent 83d3670 commit ff44fcb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
15 changes: 10 additions & 5 deletions packages/transducers/src/rfn/group-by-map.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { GroupByOpts, Reducer } from "../api";
import { __groupByOpts } from "../internal/group-opts";
import { $$reduce, reducer } from "../reduce";
import { $$reduce } from "../reduce";

// prettier-ignore
export function groupByMap<SRC, KEY, GROUP>(opts?: Partial<GroupByOpts<SRC, KEY, GROUP>>): Reducer<Map<KEY, GROUP>, SRC>;
Expand All @@ -13,10 +13,15 @@ export function groupByMap<SRC, KEY, GROUP>(...args: any[]): any {
return res;
}
const opts = __groupByOpts<SRC, KEY, GROUP>(args[0]);
const [init, _, reduce] = opts.group;
_; // ignore
return reducer<Map<KEY, GROUP>, SRC>(
const [init, complete, reduce] = opts.group;
return <Reducer<Map<KEY, GROUP>, SRC>>[
() => new Map(),
(acc) => {
for (let k of acc.keys()) {
acc.set(k, complete(acc.get(k)!));
}
return acc;
},
(acc, x) => {
const k = opts.key(x);
return acc.set(
Expand All @@ -26,5 +31,5 @@ export function groupByMap<SRC, KEY, GROUP>(...args: any[]): any {
: <GROUP>reduce(init(), x)
);
}
);
];
}
15 changes: 10 additions & 5 deletions packages/transducers/src/rfn/group-by-obj.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IObjectOf } from "@thi.ng/api";
import { GroupByOpts, Reducer } from "../api";
import { __groupByOpts } from "../internal/group-opts";
import { $$reduce, reducer } from "../reduce";
import { $$reduce } from "../reduce";

// prettier-ignore
export function groupByObj<SRC, GROUP>(opts?: Partial<GroupByOpts<SRC, PropertyKey, GROUP>>): Reducer<IObjectOf<GROUP>, SRC>;
Expand All @@ -14,16 +14,21 @@ export function groupByObj<SRC, GROUP>(...args: any[]): any {
return res;
}
const opts = __groupByOpts<SRC, PropertyKey, GROUP>(args[0]);
const [_init, _, _reduce] = opts.group;
_; // ignore
return reducer<IObjectOf<GROUP>, SRC>(
const [_init, complete, _reduce] = opts.group;
return <Reducer<IObjectOf<GROUP>, SRC>>[
() => ({}),
(acc) => {
for (let k in acc) {
acc[k] = complete(acc[k]);
}
return acc;
},
(acc, x: SRC) => {
const k: any = opts.key(x);
acc[k] = acc[k]
? <GROUP>_reduce(acc[k], x)
: <GROUP>_reduce(_init(), x);
return acc;
}
);
];
}

0 comments on commit ff44fcb

Please sign in to comment.