Skip to content

Cookbook

fold

Adds all the numbers
const sum = fold(add, 0);

sum([1, 2, 3]);
//=> 6

sum({a: 4, b: 5, c: 6});
//=> 15
Finds the youngest person
const youngest =
  fold( (prev, cur) => prev == null || prev.age > cur.age ? cur : prev, null);

youngest([ {name: 'john', age: 21}
         , {name: 'jake', age: 18}
         , {name: 'jill', age: 37}]);

//=> {name: 'jake', age: 18}

on

A case-insensitive string equality function
const streqi = on(eq, lower);

streqi('foo', 'FOO');
//=> true

unfold

Generate ten multiples of 5
const range = unfold(lte(10), mult(5), inc);

range(1);
//=> [5,10,15,20,25,30,35,40,45,50]
Numbers between 10 and 0 included
const range = unfold(gte(0), identity, dec);

range(10);
//=> [10,9,8,7,6,5,4,3,2,1,0]