Skip to content

Instantly share code, notes, and snippets.

@ivenmarquardt
Created October 17, 2016 20:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivenmarquardt/37c3e9141647c07623f7a0db7fc54041 to your computer and use it in GitHub Desktop.
Save ivenmarquardt/37c3e9141647c07623f7a0db7fc54041 to your computer and use it in GitHub Desktop.
Catamorphism, fold, lazy evaluation, continuation, short circuiting, iteration
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