Skip to content

Commit

Permalink
fix(transducers): fix matchLast(), fix & update return match*() types
Browse files Browse the repository at this point in the history
- use iterator() instead of iterator1() for matchLast()
- fix return type decls for both matchFirst() / matchLast()
- update doc strings
  • Loading branch information
postspectacular committed Sep 25, 2018
1 parent 61af7e5 commit 823d828
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
20 changes: 18 additions & 2 deletions packages/transducers/src/xform/match-first.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,28 @@ import { take } from "./take";
* ```
*
* Yields none or only the first value which passed the predicate check
* and then causes early termination.
* and then causes early termination. If `src` input is given, returns
* first match found (or `undefined`). Also see `matchLast()`.
*
* ```
* matchFirst((x) => x >= 5, [3, 1, 4, 2, 6, 5])
* // 6
*
* transduce(
* comp(
* matchFirst((x) => x >= 5),
* map((x) => x * 10)
* ),
* last(),
* [3, 1, 4, 2, 6, 5]
* )
* // 60
* ```
*
* @param pred predicate function
*/
export function matchFirst<T>(pred: Predicate<T>): Transducer<T, T>;
export function matchFirst<T>(pred: Predicate<T>, src: Iterable<T>): IterableIterator<T>;
export function matchFirst<T>(pred: Predicate<T>, src: Iterable<T>): T | undefined;
export function matchFirst<T>(pred: Predicate<T>, src?: Iterable<T>): any {
return src ?
[...iterator1(matchFirst(pred), src)][0] :
Expand Down
22 changes: 19 additions & 3 deletions packages/transducers/src/xform/match-last.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Predicate } from "@thi.ng/api";

import { Transducer } from "../api";
import { comp } from "../func/comp";
import { iterator1 } from "../iterator";
import { iterator } from "../iterator";
import { filter } from "./filter";
import { takeLast } from "./take-last";

Expand All @@ -14,14 +14,30 @@ import { takeLast } from "./take-last";
* ```
*
* Yields none or only the last value which passed the predicate check.
* If `src` input is given, returns last match found (or `undefined`).
*
* ```
* matchLast((x) => x >= 5, [3, 1, 6, 5, 4, 2])
* // 5
*
* transduce(
* comp(
* matchLast((x) => x >= 5),
* map((x) => x * 10)
* ),
* last(),
* [3, 1, 4, 2, 6, 5]
* )
* // 50
* ```
*
* @param pred predicate function
* @param src
*/
export function matchLast<T>(pred: Predicate<T>): Transducer<T, T>;
export function matchLast<T>(pred: Predicate<T>, src: Iterable<T>): IterableIterator<T>;
export function matchLast<T>(pred: Predicate<T>, src: Iterable<T>): T | undefined;
export function matchLast<T>(pred: Predicate<T>, src?: Iterable<T>): any {
return src ?
iterator1(matchLast(pred), src) :
[...iterator(matchLast(pred), src)][0] :
comp(filter(pred), takeLast(1));
}

0 comments on commit 823d828

Please sign in to comment.