Skip to content

Fold fold

Performs a traditional left fold (reduce) operation, accumulating elements from a vector using a lambda function.

Syntax

(fold lambda-fn vector)

Example

 (fold (fn [acc x] (+ acc x)) [1 2 3 4 5])
15

The lambda function receives two arguments: - acc - the accumulated value - x - the current element

The accumulation proceeds: ((((1 + 2) + 3) + 4) + 5) = 15

Info

  • Takes a single vector and returns a scalar accumulated result
  • The lambda performs left fold: processes elements from left to right
  • The first element becomes the initial accumulator value
  • Use a lambda function with two parameters: (fn [acc x] ...)

Tip

For element-wise operations, use map instead

See Also

  • map - Apply a function element-wise to vectors
  • apply - Apply a function with multiple arguments