Iterator adaptors v0.1.0

Source
Lazy adaptors over any iterator — map, filter, skip, take, enumerate, zip and chain — and walking a slice.

Cursor

pub interface Cursor {
    func Next(self: &var Self) -> T?;
}

Anything that yields items of type T one at a time through Next, which for and every adaptor here drive. A type conforms by having the method, whether or not it says extend X : Cursor<T>. Only usable as a bound.

Source

Requirements

Next

func Next(self: &var Self) -> T?

The next item, or none once there are no more.

Returns: the item

Source

SliceCursor<T>

pub struct SliceCursor<T> {
    // private fields
}

A cursor over the elements of a slice, by copy. Copy.

Source

Type parameters

NameDescription
Tthe element type

Methods

Next

pub func Next(self: &var SliceCursor<T>) -> T?

The next element, or none after the last.

Returns: the element

Source

Walk<T>

pub func Walk<T>(items: T[..]) -> SliceCursor<T>

A cursor over the elements of items, so a slice can feed the adaptors.

Source

Type parameters

NameDescription
Tthe element type

Parameters

NameTypeDescription
itemsT[..]the elements, which must outlive the cursor

Returns: the cursor

Mapped<I, T, U>

pub struct Mapped<I: Cursor<T>, T, U> {
    // private fields
}

The items of a cursor, each passed through a function.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields
Uwhat the function makes of each

Methods

Next

pub func Next(self: &var Mapped<I, T, U>) -> U?

The next item, transformed.

Returns: the item

Source

Map<I, T, U>

pub func Map<I: Cursor<T>, T, U>(
    source: I,
    transform: func(T) -> U
) -> Mapped<I, T, U>

Each item of source passed through transform, as it is asked for.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields
Uwhat transform makes of each item

Parameters

NameTypeDescription
sourceIthe cursor to wrap
transformfunc(T) -> Uwhat to do to each item

Returns: the adaptor

Filtered<I, T>

pub struct Filtered<I: Cursor<T>, T> {
    // private fields
}

The items of a cursor that pass a test.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields

Methods

Next

pub func Next(self: &var Filtered<I, T>) -> T?

The next item that passes the test.

Returns: the item

Source

Filter<I, T>

pub func Filter<I: Cursor<T>, T>(
    source: I,
    keep: func(T) -> bool
) -> Filtered<I, T>

The items of source for which keep holds.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields

Parameters

NameTypeDescription
sourceIthe cursor to wrap
keepfunc(T) -> boolthe test each item must pass

Returns: the adaptor

Taken<I, T>

pub struct Taken<I: Cursor<T>, T> {
    // private fields
}

At most a number of items from the front of a cursor.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields

Methods

Next

pub func Next(self: &var Taken<I, T>) -> T?

The next item, or none once the count is reached; the wrapped cursor is not asked again after that.

Returns: the item

Source

Take<I, T>

pub func Take<I: Cursor<T>, T>(
    source: I,
    count: uint
) -> Taken<I, T>

The first count items of source, or all of them if it has fewer.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields

Parameters

NameTypeDescription
sourceIthe cursor to wrap
countuinthow many items at most

Returns: the adaptor

Skipped<I, T>

pub struct Skipped<I: Cursor<T>, T> {
    // private fields
}

The items of a cursor after a number of them.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields

Methods

Next

pub func Next(self: &var Skipped<I, T>) -> T?

The next item, passing over the skipped ones the first time it is asked.

Returns: the item

Source

Skip<I, T>

pub func Skip<I: Cursor<T>, T>(
    source: I,
    count: uint
) -> Skipped<I, T>

The items of source after its first count.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields

Parameters

NameTypeDescription
sourceIthe cursor to wrap
countuinthow many items to pass over

Returns: the adaptor

Indexed<T>

pub struct Indexed<T> {
    pub index: uint;
    pub item: T;
}

An item and its position, counting from zero. Copy when the item is.

Source

Type parameters

NameDescription
Tthe item type

Fields

NameTypeDescription
indexuintWhere the item came in the sequence.
itemTThe item.

Enumerated<I, T>

pub struct Enumerated<I: Cursor<T>, T> {
    // private fields
}

The items of a cursor with their positions.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields

Methods

Next

pub func Next(self: &var Enumerated<I, T>) -> Indexed<T>?

The next item with its position.

Returns: the item and its position

Source

Enumerate<I, T>

pub func Enumerate<I: Cursor<T>, T>(source: I) -> Enumerated<I, T>

Each item of source with its position, counting from zero.

Source

Type parameters

NameDescription
I: Cursor<T>the wrapped cursor
Twhat it yields

Parameters

NameTypeDescription
sourceIthe cursor to wrap

Returns: the adaptor

Pair<A, B>

pub struct Pair<A, B> {
    pub first: A;
    pub second: B;
}

One item from each of two cursors. Copy when both are.

Source

Type parameters

NameDescription
Athe first item type
Bthe second item type

Fields

NameTypeDescription
firstAThe item from the first cursor.
secondBThe item from the second cursor.

Zipped<I, J, A, B>

pub struct Zipped<I: Cursor<A>, J: Cursor<B>, A, B> {
    // private fields
}

The items of two cursors side by side.

Source

Type parameters

NameDescription
I: Cursor<A>the first cursor
J: Cursor<B>the second cursor
Awhat the first yields
Bwhat the second yields

Methods

Next

pub func Next(self: &var Zipped<I, J, A, B>) -> Pair<A, B>?

The next item of each, or none once either runs out.

Returns: the pair

Source

Zip<I, J, A, B>

pub func Zip<I: Cursor<A>, J: Cursor<B>, A, B>(
    first: I,
    second: J
) -> Zipped<I, J, A, B>

The items of first and second in pairs, as long as both last.

Source

Type parameters

NameDescription
I: Cursor<A>the first cursor
J: Cursor<B>the second cursor
Awhat the first yields
Bwhat the second yields

Parameters

NameTypeDescription
firstIthe first cursor
secondJthe second cursor

Returns: the adaptor

Chained<I, J, T>

pub struct Chained<I: Cursor<T>, J: Cursor<T>, T> {
    // private fields
}

The items of one cursor and then of another.

Source

Type parameters

NameDescription
I: Cursor<T>the first cursor
J: Cursor<T>the second cursor
Twhat both yield

Methods

Next

pub func Next(self: &var Chained<I, J, T>) -> T?

The next item of the first cursor, then of the second once the first has run out.

Returns: the item

Source

Chain<I, J, T>

pub func Chain<I: Cursor<T>, J: Cursor<T>, T>(
    first: I,
    second: J
) -> Chained<I, J, T>

The items of first followed by the items of second.

Source

Type parameters

NameDescription
I: Cursor<T>the first cursor
J: Cursor<T>the second cursor
Twhat both yield

Parameters

NameTypeDescription
firstIthe cursor read first
secondJthe cursor read after it

Returns: the adaptor