query
On this page

foldl

lib.lists.foldl

Docs pulled from | This Revision | 21 minutes ago


“left fold”, like foldr, but from the left:

foldl op nul [x_1 x_2 ... x_n] == op (... (op (op nul x_1) x_2) ... x_n).

Inputs

op

1. Function argument

nul

2. Function argument

list

3. Function argument

Type

foldl :: (b -> a -> b) -> b -> [a] -> b

Examples

lib.lists.foldl usage example

lconcat = foldl (a: b: a + b) "z"
lconcat [ "a" "b" "c" ]
=> "zabc"
# different types
lstrange = foldl (str: int: str + toString (int + 1)) "a"
lstrange [ 1 2 3 4 ]
=> "a2345"

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

foldl = op: nul: list:
    let
      foldl' = n:
        if n == -1
        then nul
        else op (foldl' (n - 1)) (elemAt list n);
    in foldl' (length list - 1);