Skip to content
Snippets Groups Projects
Commit c8d0cfc4 authored by David Sehnal's avatar David Sehnal
Browse files

Iterator.forEach tweak

parent e70a0add
No related branches found
No related tags found
No related merge requests found
...@@ -107,11 +107,11 @@ namespace Iterator { ...@@ -107,11 +107,11 @@ namespace Iterator {
export function map<T, R>(base: Iterator<T>, f: (v: T) => R): Iterator<R> { return new MapIteratorImpl(base, f); } export function map<T, R>(base: Iterator<T>, f: (v: T) => R): Iterator<R> { return new MapIteratorImpl(base, f); }
export function filter<T>(base: Iterator<T>, p: (v: T) => boolean): Iterator<T> { return new FilterIteratorImpl(base, p); } export function filter<T>(base: Iterator<T>, p: (v: T) => boolean): Iterator<T> { return new FilterIteratorImpl(base, p); }
// f can return non-undefined falsy value to stop the iteration. // Iterate until first truthy value is returned.
export function forEach<T, Ctx>(it: Iterator<T>, f: (v: T, ctx: Ctx) => boolean | void, ctx: Ctx): Ctx { export function forEach<T, Ctx>(it: Iterator<T>, f: (v: T, ctx: Ctx) => any, ctx: Ctx): Ctx {
while (it.hasNext) { while (it.hasNext) {
const c = f(it.move(), ctx); const c = f(it.move(), ctx);
if (typeof c !== 'undefined' && !c) return ctx; if (c) return ctx;
} }
return ctx; return ctx;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment