Skip to content

Instantly share code, notes, and snippets.

@lambdaydoty
Created April 1, 2020 10:49
Show Gist options
  • Save lambdaydoty/0abeb61f13090e745b46a63a32efa317 to your computer and use it in GitHub Desktop.
Save lambdaydoty/0abeb61f13090e745b46a63a32efa317 to your computer and use it in GitHub Desktop.
function foldr (c, h) {
return ([a, ...x]) => (a === undefined)
? c
: h (a) (foldr(c, h)(x))
}
function foldl (c, h) {
return ([a, ...x]) => (a === undefined)
? c
: foldl (h(c)(a), h) (x)
}
const sum1 = foldr(0, x => y => { console.log(`${x} + ${y}`); return x + y })
const sum2 = foldl(0, x => y => { console.log(`${x} + ${y}`); return x + y })
const cat1 = foldr('0', x => y => `(${x} + ${y})`)
const cat2 = foldl('0', x => y => `(${x} + ${y})`)
console.log(
cat1([1, 2, 3, 4, 5])
)
console.log(
cat2([1, 2, 3, 4, 5])
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment