fold
fn fold(Vec<int> xs, int init, int -> int -> int folder) -> int Import
Source: use collections::vec::{fold}; src/collections/vec.hy Combines the elements left to right: starts from init and replaces the accumulator with folder(acc)(x) for each x.
folder is curried: it takes the accumulator and returns a function of the
element. Only Vec<int> is supported for now.
Parameters#
| Name | Type |
|---|---|
xs | Vec<int> |
init | int |
folder | int -> int -> int |
Returns#
int
Examples#
use collections::vec::{fold};
use tests::assert;
let xs: Vec<int> = [1, 2, 3, 4];
let total = fold(xs, 0, fn (int acc) => fn (int x) use (acc) => acc + x);
assert!(total == 10);
See also#
Vecfold_int: The same asfold; kept for older code.contains: Returnstruewhenneedleis inxs.index_of: Returns the index of the firstneedleinxs, or-1.binary_search: Findsneedlein the ascending vectorxsby halving, and returns an index holding it.dedup: Returnsxswith runs of equal neighbours collapsed to one value.map: Returns a new vector withmapperapplied to each element, in order.filter: Returns the elements for whichpredistrue, in order.