Module std::iter

Iterator combinators for Vec<T> and any Iterator implementation.

This module exposes the lazy Iterator-based combinator API (Map, Filter, Take, Skip, and the terminal helpers fold, count, collect). Each wrapper is a pub type record that itself implements the Iterator trait declared in std/builtins.hew, so adapters compose through chained constructor calls:

import std.iter;

fn main() {
    var v: Vec<i64> = Vec.new();
    v.push(1);
    v.push(2);
    v.push(3);
    let it = v.iter();
    let doubled_first_two = iter.take(iter.map(it, |x: i64| x * 2), 2);
    println(iter.collect(doubled_first_two).len());  // 2
}

Iterator.next takes a mutable receiver (fn next(var self) -> Option<Self.Item>). A manually driven iterator needs a var binding; a for loop advances it automatically. Adapters and terminal helpers consume their input iterator. After passing an iterator to an adapter, use the returned adapter to continue.

Lazy combinator surface

WrapperConstructorItem projection
Map<I,A,B>map(it, f)B from fn(A) -> B
Filter<I,A>filter(it, pred)A (subset of inner)
Take<I,A>take(it, n)inner's Item (A)
Skip<I,A>skip(it, n)inner's Item (A)

Terminal helpers (fold, count, collect, any, all, sum, sum_f64, product, product_f64) consume any Iterator. Callers drive a Vec<T> through the lazy surface via Vec.into_iter(), e.g. iter.collect(iter.map(v.into_iter(), f)).

Contents

Functions

Function map

pub fn map(it: I, f: fn(A) -> B) -> Map<I, A, B>

Construct a lazy Map adapter over it applying f to each item.

Function filter

pub fn filter(it: I, pred: fn(A) -> bool) -> Filter<I, A>

Construct a lazy Filter adapter over it keeping items for which pred returns true.

Function take

pub fn take(it: I, n: i64) -> Take<I, A>

Construct a lazy Take adapter that yields at most n items from it.

Function skip

pub fn skip(it: I, n: i64) -> Skip<I, A>

Construct a lazy Skip adapter that discards the first n items from it.

Function enumerate

pub fn enumerate(it: I) -> Enumerate<I, A>

Construct a lazy Enumerate adapter over it.

Function fold

pub fn fold(it: I, init: B, f: fn(B, A) -> B) -> B

Left-fold over it: walk every item applying f(acc, item) and return the accumulated value.

Function count

pub fn count(it: I) -> i64

Return the number of items produced by it.

Function collect

pub fn collect(it: I) -> Vec<A>

Collect every item produced by it into a Vec<A>.

Function any

pub fn any(it: I, f: fn(A) -> bool) -> bool

Return true if f returns true for at least one item produced by it. Short-circuits on the first match.

Function all

pub fn all(it: I, f: fn(A) -> bool) -> bool

Return true if f returns true for every item produced by it. Short-circuits on the first non-match.

Function find

pub fn find(it: I, pred: fn(A) -> bool) -> Option<A>

Return the first item for which pred returns true, or None.

Like any, this short-circuits as soon as the predicate matches; items observed before the match are released by their lexical match arms.

Function sum

pub fn sum(it: I) -> i64

Return the sum of every i64 item produced by it.

Function sum_f64

pub fn sum_f64(it: I) -> f64

Return the sum of every f64 item produced by it.

Function product

pub fn product(it: I) -> i64

Return the product of every i64 item produced by it. Returns 1 for an empty iterator (the multiplicative identity).

Function product_f64

pub fn product_f64(it: I) -> f64

Return the product of every f64 item produced by it. Returns 1.0 for an empty iterator (the multiplicative identity).

Types

Struct Map

Lazy map adapter: yields f(x) for each item produced by the inner iterator.

A is the inner Item type (carried in the type parameters so the closure's input projection is explicit); B is the produced Item.

Fields

iter: I
f: fn(A) -> B

Struct Filter

Lazy filter adapter: yields only those items for which pred returns true. Inner Item and produced Item agree on A.

Fields

iter: I
pred: fn(A) -> bool

Struct Take

Lazy take adapter: yields at most remaining items from the inner iterator, then reports None.

A is the inner Item type. It is carried as a direct type parameter (rather than left projection-only in the impl bound) so a concrete site — Take<Countdown, i64> — pins the item type in the record arguments, which is what the cross-module monomorphisation collector keys on. Map/Filter carry their item type the same way; without it a projection-only A stays free and terminals (count/collect/fold) fail to monomorphise (mir-gap-where-clause-proj-monomorph).

Fields

iter: I
remaining: i64

Struct Skip

Lazy skip adapter: discards the first remaining items, then yields the rest of the inner iterator unchanged.

A is the inner Item type, carried as a direct type parameter for the same monomorphisation reason as Take (see above): a projection- only item param cannot be pinned by the collector, so terminals over Skip fail to monomorphise.

Fields

iter: I
remaining: i64

Struct Enumerate

Lazy enumerate adapter: pairs each item with its zero-based position.

The index advances only when the inner iterator yields an item, so an exhausted iterator remains exhausted without manufacturing a final index.

Fields

iter: I
index: i64