Iterator adaptors v0.1.0
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.
Requirements
Next
func Next(self: &var Self) -> T?
The next item, or none once there are no more.
Returns: the item
SliceCursor<T>
pub struct SliceCursor<T> {
// private fields
}
A cursor over the elements of a slice, by copy. Copy.
Type parameters
| Name | Description |
|---|---|
T | the element type |
Methods
Next
pub func Next(self: &var SliceCursor<T>) -> T?
The next element, or none after the last.
Returns: the element
Walk<T>
pub func Walk<T>(items: T[..]) -> SliceCursor<T>
A cursor over the elements of items, so a slice can feed the adaptors.
Type parameters
| Name | Description |
|---|---|
T | the element type |
Parameters
| Name | Type | Description |
|---|---|---|
items | T[..] | 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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what it yields |
U | what the function makes of each |
Methods
Next
pub func Next(self: &var Mapped<I, T, U>) -> U?
The next item, transformed.
Returns: the item
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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what it yields |
U | what transform makes of each item |
Parameters
| Name | Type | Description |
|---|---|---|
source | I | the cursor to wrap |
transform | func(T) -> U | what 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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what it yields |
Methods
Next
pub func Next(self: &var Filtered<I, T>) -> T?
The next item that passes the test.
Returns: the item
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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what it yields |
Parameters
| Name | Type | Description |
|---|---|---|
source | I | the cursor to wrap |
keep | func(T) -> bool | the 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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what 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
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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what it yields |
Parameters
| Name | Type | Description |
|---|---|---|
source | I | the cursor to wrap |
count | uint | how 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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what 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
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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what it yields |
Parameters
| Name | Type | Description |
|---|---|---|
source | I | the cursor to wrap |
count | uint | how 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.
Type parameters
| Name | Description |
|---|---|
T | the item type |
Fields
| Name | Type | Description |
|---|---|---|
index | uint | Where the item came in the sequence. |
item | T | The item. |
Enumerated<I, T>
pub struct Enumerated<I: Cursor<T>, T> {
// private fields
}
The items of a cursor with their positions.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what 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
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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the wrapped cursor |
T | what it yields |
Parameters
| Name | Type | Description |
|---|---|---|
source | I | the 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.
Type parameters
| Name | Description |
|---|---|
A | the first item type |
B | the second item type |
Fields
| Name | Type | Description |
|---|---|---|
first | A | The item from the first cursor. |
second | B | The 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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<A> | the first cursor |
J: Cursor<B> | the second cursor |
A | what the first yields |
B | what 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
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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<A> | the first cursor |
J: Cursor<B> | the second cursor |
A | what the first yields |
B | what the second yields |
Parameters
| Name | Type | Description |
|---|---|---|
first | I | the first cursor |
second | J | the 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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the first cursor |
J: Cursor<T> | the second cursor |
T | what 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
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.
Type parameters
| Name | Description |
|---|---|
I: Cursor<T> | the first cursor |
J: Cursor<T> | the second cursor |
T | what both yield |
Parameters
| Name | Type | Description |
|---|---|---|
first | I | the cursor read first |
second | J | the cursor read after it |
Returns: the adaptor