-
-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transducers): add mapcatIndexed() xform
- Loading branch information
1 parent
933920d
commit 4f3d6e0
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} |