Created
October 17, 2016 20:05
-
-
Save ivenmarquardt/37c3e9141647c07623f7a0db7fc54041 to your computer and use it in GitHub Desktop.
Catamorphism, fold, lazy evaluation, continuation, short circuiting, iteration
This file contains hidden or 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
const foldlk = f => acc => xs => { | |
const next = (acc, key) => xs.length === key | |
? acc | |
: f(acc) (xs[key], acc => next(acc, key + 1)); | |
return next(acc, 0); | |
}; | |
const every = f => foldlk(x => (y, k) => f(y) ? k(true) : false) (true); | |
const some = f => foldlk(x => (y, k) => f(y) ? true : k(false)) (false); | |
const take = n => foldlk(acc => (x, k) => acc.length === n ? acc : (acc.push(x), k(acc))) ([]); | |
every(x => x % 2 === 0) ([2,4,6,8]); // yields true | |
some(x => x % 2 === 0) ([1,2,3,5]); // yields true and stops early | |
take(3) ([1,2,3,4,5]); // yields [1,2,3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment