Skip to main content

Range.fold

foldinitfunction

Documentation

Combines all the elements of the range, by iteratively applying the passed function with next elements of the range.

In general, the result of Range start end step . fold init f is the same as f (...(f (f init start) start+step)...) last

Arguments

  • init: The initial value for the fold.
  • function: A binary function taking an item and a number, and returning an item.

Examples

In the following example, we'll compute the sum of all even integers

less than 100.

      0.up_to 100 . with_step 2 . fold 0 (+)