import { I } from "https://deno.land/x/ahh@v0.10.0/src/iter/mod.ts";
Functionality for Iterator
.
Static Methods
Consumes an Iterator
and returns the number of iterations.
Examples
import { I } from "./mod.ts";
const iter = I.iter(["hello", "there", "world"]);
console.log(I.count(iter)); // 3
console.log(iter.next()); // undefined
Creates an Iterator
that returns nothing.
Examples
import { I } from "./mod.ts";
const iter = I.empty();
console.log(iter.next()); // undefined
console.log(iter.next()); // undefined
Creates an Iterator
which returns the current iteration as well
as the next value.
Examples
import { I } from "./mod.ts";
const iter = I.enumerate(I.iter(["hello", "there", "world"]));
console.log(iter.next()); // [0, "hello"]
console.log(iter.next()); // [1, "there"]
console.log(iter.next()); // [2, "world"]
console.log(iter.next()); // undefined
Creates an Iterator
that returns items when f
returns true
.
Examples
import { I } from "./mod.ts";
const iter = I.filter(I.iter([1, 2, 3, 4]), (i) => i % 2 === 0);
console.log(iter.next()); // 2
console.log(iter.next()); // 4
console.log(iter.next()); // undefined
Consumes an Iterator
until f
returns true
and returns the
item.
Examples
import { I } from "./mod.ts";
const iter = I.iter([1, 2, 3]);
console.log(I.find(iter, (i) => i > 1)); // 2
console.log(iter.next()); // 3
console.log(iter.next()); // undefined
Creates an Iterator
that flattens a layer of nested
Iterator
s.
Examples
import { I } from "./mod.ts";
const iter = I.flatten(I.iter([I.once(1), I.empty(), I.iter([2, 3])]));
console.log(iter.next()); // 1
console.log(iter.next()); // 2
console.log(iter.next()); // 3
console.log(iter.next()); // undefined
Creates an Iterator
where each iteration calls f
.
Examples
import { I } from "./mod.ts";
const iter = I.fn(() => 1);
console.log(iter.next()); // 1
console.log(iter.next()); // 1
// ...
Consumes an Iterator
and folds every item into an accumulator
using f
.
Examples
import { I } from "./mod.ts";
const iter = I.iter([1, 2, 3]);
console.log(I.fold(iter, 0, (a, b) => a + b)); // 6
console.log(iter.next()); // undefined
Consumes an Iterator
and runs f
on each item.
Examples
import { I } from "./mod.ts";
const iter = I.iter([1, 2, 3]);
I.forEach(iter, (i) => console.log(i));
console.log(iter.next()); // undefined
Creates an Iterator
from an Iterable
.
Examples
import { I } from "./mod.ts";
const iter = I.iter([1, 2]);
console.log(iter.next()); // 1
console.log(iter.next()); // 2
console.log(iter.next()); // undefined
Consumes an Iterator
and returns the last item.
Examples
import { I } from "./mod.ts";
const iter = I.iter([1, 2, 3]);
console.log(I.last(iter)); // 3
console.log(iter.next()); // undefined
Creates an Iterator
that calls f
on each item.
Examples
import { I } from "./mod.ts";
const iter = I.map(I.iter([1, 2, 3]), (i) => i * i);
console.log(iter.next()); // 1
console.log(iter.next()); // 4
console.log(iter.next()); // 9
console.log(iter.next()); // undefined
Consumes an Iterator
up to n
and returns the item.
Examples
import { I } from "./mod.ts";
const iter = I.iter([1, 2, 3]);
console.log(I.nth(iter, 1)); // 2
console.log(iter.next()); // 3
console.log(iter.next()); // undefined
Creates an Iterator
that returns exactly one item.
Examples
import { I } from "./mod.ts";
const iter = I.once(1);
console.log(iter.next()); // 1
console.log(iter.next()); // undefined
Creates an Iterator
that is Peekable
.
Examples
import { I } from "./mod.ts";
const iter = I.peekable(I.iter([1, 2, 3]));
console.log(iter.next()); // 1
console.log(iter.peek()); // 2
console.log(iter.next()); // 2
console.log(iter.next()); // 3
console.log(iter.peek()); // undefined
console.log(iter.next()); // undefined
Creates an Iterator
that endlessly returns an item.
Example
import { I } from "./mod.ts";
const iter = I.repeat(1);
console.log(iter.next()); // 1
console.log(iter.next()); // 1
console.log(iter.next()); // 1
Creates an Iterator
that skips the first n
items.
Examples
import { I } from "./mod.ts";
const iter = I.skip(I.iter([1, 2, 3]), 1);
console.log(iter.next()); // 2
console.log(iter.next()); // 3
console.log(iter.next()); // undefined
Creates an Iterator
that skips items while f
return true
.
Examples
import { I } from "./mod.ts";
const iter = I.skipWhile(I.iter([1, 3, 2, 3]), (i) => i % 2 !== 0);
console.log(iter.next()); // 2
console.log(iter.next()); // 3
console.log(iter.next()); // undefined
Creates an Iterator
where each successive item is computed from
the preceding one.
Examples
import { I } from "./mod.ts";
const iter = I.successors(0, (i) => i + 1);
console.log(iter.next()); // 0
console.log(iter.next()); // 1
console.log(iter.next()); // 2
console.log(iter.next()); // 3
Creates an Iterator
that returns the first n
items.
Examples
import { I } from "./mod.ts";
const iter = I.take(I.iter([1, 2, 3]), 2);
console.log(iter.next()); // 1
console.log(iter.next()); // 2
console.log(iter.next()); // undefined
Creates an Iterator
that returns items while f
returns
true
.
Examples
import { I } from "./mod.ts";
const iter = I.takeWhile(I.iter([4, 2, 3, 4]), (i) => i % 2 === 0);
console.log(iter.next()); // 4
console.log(iter.next()); // 2
console.log(iter.next()); // undefined
console.log(iter.next()); // undefined
Creates an Iterator
that will iterate over two other
Iterator
s simultaneously.
If either Iterator
returns None
, so will this
Iterator
.
Examples
import { I } from "./mod.ts"
const iter = I.zip(I.iter([1, 2]), I.iter(["3", "4", "5"]));
console.log(iter.next()); // [1, "3"]
console.log(iter.next()); // [2, "4"]
console.log(iter.next()); // undefined