Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/ahh/src/iter/mod.ts>I

Idiomatic type-safety structures for TypeScript.
Go to Latest
class I
import { I } from "https://deno.land/x/ahh@v0.10.0/src/iter/mod.ts";

Functionality for Iterator.

Static Methods

chain<T>(iterA: Iterator<T>, iterB: Iterator<T>): Iterator<T>

Creates an Iterator that will iterate over two other Iterators sequentially.

Examples

import { I } from "./mod.ts";

const iter = I.chain(I.iter([1, 2]), I.once(3));

console.log(iter.next()); // 1
console.log(iter.next()); // 2
console.log(iter.next()); // 3
console.log(iter.next()); // undefined
count<T>(iter: Iterator<T>): number

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
empty<T>(): Iterator<T>

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
enumerate<T>(iter: Iterator<T>): Iterator<[number, T]>

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
filter<T>(iter: Iterator<T>, f: (_: T) => boolean): Iterator<T>

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
find<T>(iter: Iterator<T>, f: (_: T) => boolean): Option<T>

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
flatten<T>(iter: Iterator<Iterator<T>>): Iterator<T>

Creates an Iterator that flattens a layer of nested Iterators.

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
fn<T>(f: Iterator<T>["next"]): Iterator<T>

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
// ...
fold<T, U>(
iter: Iterator<T>,
init: U,
f: (_: U, _1: T) => U,
): U

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
forEach<T>(iter: Iterator<T>, f: (_: T) => void): void

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
iter<T>(iter: Iterable<T>): Iterator<T>

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
last<T>(iter: Iterator<T>): Option<T>

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
map<T, U>(iter: Iterator<T>, f: (_: T) => U): Iterator<U>

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
nth<T>(iter: Iterator<T>, n: number): Option<T>

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
once<T>(item: T): Iterator<T>

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
peekable<T>(iter: Iterator<T>): Peekable<T>

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
repeat<T>(item: T): Iterator<T>

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
skip<T>(iter: Iterator<T>, n: number): Iterator<T>

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
skipWhile<T>(iter: Iterator<T>, f: (_: T) => boolean): Iterator<T>

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
successors<T>(first: Option<T>, f: (_: T) => Option<T>)

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
take<T>(iter: Iterator<T>, n: number): Iterator<T>

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
takeWhile<T>(iter: Iterator<T>, f: (_: T) => boolean)

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
zip<T, U>(iterA: Iterator<T>, iterB: Iterator<U>): Iterator<[T, U]>

Creates an Iterator that will iterate over two other Iterators 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