Skip to content

Commit

Permalink
feat(transducers): add mapcatIndexed() xform
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Sep 18, 2020
1 parent 933920d commit 4f3d6e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/transducers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export * from "./xform/map-nth";
export * from "./xform/map-vals";
export * from "./xform/map";
export * from "./xform/mapcat";
export * from "./xform/mapcat-indexed";
export * from "./xform/match-first";
export * from "./xform/match-last";
export * from "./xform/moving-average";
Expand Down
37 changes: 37 additions & 0 deletions packages/transducers/src/xform/mapcat-indexed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Fn2 } from "@thi.ng/api";
import type { Transducer } from "../api";
import { comp } from "../func/comp";
import { $iter, iterator } from "../iterator";
import { cat } from "./cat";
import { mapIndexed } from "./map-indexed";

/**
* Transducer. Similar to {@link (mapcat:1)}, but given `fn` takes two
* arguments: `index` and `value` to transform.
*
* @remarks
* An optional start index `offset` can be provided (default 0). Also see
* {@link (mapIndexed:1)}.
*
* @param fn - transformation function
* @param offset - initial index
*/
export function mapcatIndexed<A, B>(
fn: Fn2<number, A, Iterable<B> | null | undefined>,
offset?: number
): Transducer<A, B>;
export function mapcatIndexed<A, B>(
fn: Fn2<number, A, Iterable<B> | null | undefined>,
src: Iterable<A>
): IterableIterator<B>;
export function mapcatIndexed<A, B>(
fn: Fn2<number, A, Iterable<B> | null | undefined>,
offset: number,
src: Iterable<A>
): IterableIterator<B>;
export function mapcatIndexed<A, B>(...args: any[]): any {
return (
$iter(mapcatIndexed, args, iterator) ||
comp(mapIndexed<A, Iterable<B>>(args[0], args[1]), cat())
);
}

0 comments on commit 4f3d6e0

Please sign in to comment.