-
-
Notifications
You must be signed in to change notification settings - Fork 151
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 length() transducer
- Loading branch information
1 parent
755ca21
commit 47a95b7
Showing
2 changed files
with
34 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,33 @@ | ||
import { isIterable } from "@thi.ng/checks/is-iterable"; | ||
import type { Transducer } from "./api.js"; | ||
import { iterator1 } from "./iterator.js"; | ||
import { map } from "./map.js"; | ||
import type { ILength } from "@thi.ng/api"; | ||
|
||
/** | ||
* Similar to `map((x) => x.length)`. A transducer which returns the `.length` | ||
* of each input (optionally with offset `n` added, default: 0) and yields | ||
* sequence of these values. | ||
* | ||
* @example | ||
* ```ts | ||
* [...length(0, ["a", "bc", "def"])] | ||
* // [1, 2, 3] | ||
* ``` | ||
* | ||
* @param n - optional offset | ||
*/ | ||
export function length(n?: number): Transducer<ILength, number>; | ||
export function length( | ||
n: number, | ||
src: Iterable<ILength> | ||
): IterableIterator<number>; | ||
export function length(n = 0, src?: Iterable<ILength>): any { | ||
return isIterable(src) | ||
? iterator1(length(n), src) | ||
: map( | ||
n === 0 | ||
? (x: ILength) => x.length | ||
: (x: ILength) => x.length + n | ||
); | ||
} |