js 模拟haskell 的foldl

haskell

foldl :: (a -> b -> a) -> a -> [b] -> a
foldl step zero (x:xs) = foldl step (step zero x) xs
foldl _ zero []        = zero

js

function foldl(step, zero, arr) {
    return arr.length > 0 ? foldl(step, step(zero, arr.shift()), arr) : zero
}

console.log(foldl((x, y)=> x + y, 1, [1,2,3,4,5]))
``

你可能感兴趣的:(js 模拟haskell 的foldl)