Function in collections::vec coil-stdlib

fold

fn fold(Vec<int> xs, int init, int -> int -> int folder) -> int
Import
use collections::vec::{fold};
Source: 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#

NameType
xsVec<int>
initint
folderint -> 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#

  • Vec
  • fold_int: The same as fold; kept for older code.
  • contains: Returns true when needle is in xs.
  • index_of: Returns the index of the first needle in xs, or -1.
  • binary_search: Finds needle in the ascending vector xs by halving, and returns an index holding it.
  • dedup: Returns xs with runs of equal neighbours collapsed to one value.
  • map: Returns a new vector with mapper applied to each element, in order.
  • filter: Returns the elements for which pred is true, in order.
↑ ↓ navigate ↵ open