// implement foldl (reduceLeft)
function foldl(arr, f, acc) {
if (arr.length === 0) return acc
const [x, ...xs] = arr
return foldl(xs, f, f(acc, x))
}
# driver code
arr = [1,2,3,4,5]
parted = foldl(arr, (acc, x) => {
const [a, b] = acc
return (x % 2)
? [[...a, x], [...b]]
: [[...a], [...b, x]]
}, [[], []])
console.log(foldl(arr, (acc, x) => acc + x, 0))
console.log(foldl(arr, (acc, x) => acc * x, 1))
console.log(parted)
To embed this project on your website, copy the following code and paste it into your website's HTML: