Skip to content

Commit

Permalink
feat(transducers): add mapVals() xform
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Mar 19, 2018
1 parent 3bc8d54 commit abc195a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/transducers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,9 @@ reducer and optional initial accumulator/result.

#### `mapNth<A, B>(n: number, offset: number, fn: (x: A) => B): Transducer<A, B>`

#### `mapVals<A, B>(fn: (v: A) => B, copy = true): Transducer<IObjectOf<A>, IObjectOf<B>>`


#### `movingAverage(n: number): Transducer<number, number>`

#### `movingMedian<A, B>(n: number, key?: ((x: A) => B), cmp?: Comparator<B>): Transducer<A, A>`
Expand Down
3 changes: 2 additions & 1 deletion packages/transducers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ export * from "./xform/interleave";
export * from "./xform/interpose";
export * from "./xform/keep";
export * from "./xform/labeled";
export * from "./xform/map-deep";
export * from "./xform/map-indexed";
export * from "./xform/map-keys";
export * from "./xform/map-nth";
export * from "./xform/map-vals";
export * from "./xform/map";
export * from "./xform/mapcat";
export * from "./xform/map-deep";
export * from "./xform/moving-average";
export * from "./xform/moving-median";
export * from "./xform/multiplex";
Expand Down
13 changes: 13 additions & 0 deletions packages/transducers/src/xform/map-vals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { IObjectOf } from "@thi.ng/api/api";
import { Transducer } from "../api";
import { map } from "./map";

export function mapVals<A, B>(fn: (v: A) => B, copy = true): Transducer<IObjectOf<A>, IObjectOf<B>> {
return map((x) => {
const res: any = copy ? { ...x } : x;
for (let k in x) {
res[k] = fn(x[k]);
}
return <any>res;
});
}

0 comments on commit abc195a

Please sign in to comment.