std::iterIterator 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:
let doubled_first_two = iter::take(iter::map(it, |x: i64| x * 2), 2);
Iterator::next takes a mutable receiver (fn next(var self) -> Option<Self::Item>).
Call sites write the returned Self state back into the caller binding; the
for-loop desugar that will introduce and drive that binding lands in a later
stage of the iteration-foundation lane.
| Wrapper | Constructor | Item 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> | take(it, n) | inner's Item |
Skip<I> | skip(it, n) | inner's Item |
Terminal helpers (fold, count, collect) consume any Iterator.
The legacy eager helpers below (map_int, filter_str, ...) are kept
transiently while the Vec<T>: IntoIterator impl is being wired in a
follow-on stage; once Vec::into_iter lands, callers migrate to
iter::map(v.into_iter(), ...) and the eager table is retired in a
single subtractive commit.
mapConstruct a lazy Map adapter over it applying f to each item.
filterConstruct a lazy Filter adapter over it keeping items for which
pred returns true.
takeConstruct a lazy Take adapter that yields at most n items from
it.
skipConstruct a lazy Skip adapter that discards the first n items
from it.
foldLeft-fold over it: walk every item applying f(acc, item) and
return the accumulated value.
countReturn the number of items produced by it.
collectCollect every item produced by it into a Vec<A>.
map_intApply f to every element and return a new vector of results.
let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
let doubled = iter.map_int(v, |x: i64| x * 2);
// doubled == [2, 4]
filter_intReturn a new vector containing only elements for which f returns true.
let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
let evens = iter.filter_int(v, |x: i64| x % 2 == 0);
// evens == [2]
fold_intLeft-fold: accumulate all elements starting from init.
let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
let total = iter.fold_int(v, 0, |acc: i64, x: i64| acc + x);
// total == 6
anyReturn true if f returns true for at least one element.
let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
println(iter.any(v, |x: i64| x > 2)); // true
println(iter.any(v, |x: i64| x > 9)); // false
allReturn true if f returns true for every element.
let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
println(iter.all(v, |x: i64| x > 0)); // true
println(iter.all(v, |x: i64| x > 2)); // false
sumReturn the sum of all elements.
let v: Vec<i64> = Vec::new();
v.push(1);
v.push(2);
v.push(3);
println(iter.sum(v)); // 6
map_strApply f to every element and return a new vector of results.
let v: Vec<string> = Vec::new();
v.push("hello");
v.push("world");
let shouted = iter.map_str(v, |s: string| s + "!");
// shouted == ["hello!", "world!"]
filter_strReturn a new vector containing only elements for which f returns true.
let v: Vec<string> = Vec::new();
v.push("foo");
v.push("bar");
v.push("baz");
let bs = iter.filter_str(v, |s: string| s.starts_with("b"));
// bs == ["bar", "baz"]
fold_strLeft-fold: accumulate all elements starting from init.
let v: Vec<string> = Vec::new();
v.push("a");
v.push("b");
v.push("c");
let joined = iter.fold_str(v, "", |acc: string, s: string| acc + s);
// joined == "abc"
any_strReturn true if f returns true for at least one element.
let v: Vec<string> = Vec::new();
v.push("foo");
v.push("bar");
println(iter.any_str(v, |s: string| s == "bar")); // true
println(iter.any_str(v, |s: string| s == "baz")); // false
all_strReturn true if f returns true for every element.
let v: Vec<string> = Vec::new();
v.push("foo");
v.push("bar");
println(iter.all_str(v, |s: string| s.len() == 3)); // true
println(iter.all_str(v, |s: string| s == "foo")); // false
map_f64Apply f to every element and return a new vector of results.
let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.0);
let doubled = iter.map_f64(v, |x: f64| x * 2.0);
// doubled == [2.0, 4.0]
filter_f64Return a new vector containing only elements for which f returns true.
let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.5);
v.push(3.0);
let big = iter.filter_f64(v, |x: f64| x > 2.0);
// big == [2.5, 3.0]
fold_f64Left-fold: accumulate all elements starting from init.
let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.0);
v.push(3.0);
let total = iter.fold_f64(v, 0.0, |acc: f64, x: f64| acc + x);
// total == 6.0
any_f64Return true if f returns true for at least one element.
let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.0);
v.push(3.0);
println(iter.any_f64(v, |x: f64| x > 2.5)); // true
println(iter.any_f64(v, |x: f64| x > 9.0)); // false
all_f64Return true if f returns true for every element.
let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.0);
v.push(3.0);
println(iter.all_f64(v, |x: f64| x > 0.0)); // true
println(iter.all_f64(v, |x: f64| x > 2.0)); // false
sum_f64Return the sum of all elements.
let v: Vec<f64> = Vec::new();
v.push(1.0);
v.push(2.5);
v.push(3.0);
println(iter.sum_f64(v)); // 6.5
take_intReturn the first n elements of v. If n exceeds the length, the
whole vector is returned.
let v: Vec<i64> = Vec::new();
v.push(10); v.push(20); v.push(30);
let t = iter.take_int(v, 2);
// t == [10, 20]
skip_intSkip the first n elements of v and return the rest. If n exceeds
the length, an empty vector is returned.
let v: Vec<i64> = Vec::new();
v.push(10); v.push(20); v.push(30);
let s = iter.skip_int(v, 1);
// s == [20, 30]
count_intReturn the number of elements for which f returns true.
let v: Vec<i64> = Vec::new();
v.push(1); v.push(2); v.push(3); v.push(4);
println(iter.count_int(v, |x: i64| x % 2 == 0)); // 2
take_strReturn the first n elements of v. If n exceeds the length, the
whole vector is returned.
let v: Vec<string> = Vec::new();
v.push("a"); v.push("b"); v.push("c");
let t = iter.take_str(v, 2);
// t == ["a", "b"]
skip_strSkip the first n elements of v and return the rest. If n exceeds
the length, an empty vector is returned.
let v: Vec<string> = Vec::new();
v.push("a"); v.push("b"); v.push("c");
let s = iter.skip_str(v, 1);
// s == ["b", "c"]
count_strReturn the number of elements for which f returns true.
let v: Vec<string> = Vec::new();
v.push("foo"); v.push("bar"); v.push("baz");
println(iter.count_str(v, |s: string| s.starts_with("b"))); // 2
take_f64Return the first n elements of v. If n exceeds the length, the
whole vector is returned.
let v: Vec<f64> = Vec::new();
v.push(1.0); v.push(2.0); v.push(3.0);
let t = iter.take_f64(v, 2);
// t == [1.0, 2.0]
skip_f64Skip the first n elements of v and return the rest. If n exceeds
the length, an empty vector is returned.
let v: Vec<f64> = Vec::new();
v.push(1.0); v.push(2.0); v.push(3.0);
let s = iter.skip_f64(v, 1);
// s == [2.0, 3.0]
count_f64Return the number of elements for which f returns true.
let v: Vec<f64> = Vec::new();
v.push(1.0); v.push(2.5); v.push(-1.0);
println(iter.count_f64(v, |x: f64| x > 0.0)); // 2
productReturn the product of all elements. Returns 1 for an empty vector
(the multiplicative identity).
let v: Vec<i64> = Vec::new();
v.push(2); v.push(3); v.push(4);
println(iter.product(v)); // 24
product_f64Return the product of all elements. Returns 1.0 for an empty vector
(the multiplicative identity).
let v: Vec<f64> = Vec::new();
v.push(2.0); v.push(0.5); v.push(4.0);
println(iter.product_f64(v)); // 4.0
MapLazy 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.
FilterLazy filter adapter: yields only those items for which pred returns
true. Inner Item and produced Item agree on A.
TakeLazy take adapter: yields at most remaining items from the inner
iterator, then reports None.
SkipLazy skip adapter: discards the first remaining items, then yields
the rest of the inner iterator unchanged.