Created
April 1, 2020 10:49
-
-
Save lambdaydoty/0abeb61f13090e745b46a63a32efa317 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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