A light introduction to
k => {k: v} => {k: v} => Boolean
function map (fn, list) { let results = []; for (let i = 0; i < list.length; i++) { results.push(fn(list[i], i)); } return results; }
let cardinalDegs = [0, 90, 180, 360]; let cardinalRads = map(MathLib.degToRad, cardinalDegs);
function partial (fn, ...boundArgs) { return (...calledArgs) => { return fn.apply(null, [...boundArgs, ...calledArgs]); }; }
let getProp = (prop, obj) => obj[prop]; let getAge = partial(getProp, 'age'); let getAges = partial(map, getAge); let people = [ {name: "Joe", age: 31}, {name: "Jack", age: 32}, {name: "Jim", age: 29} ]; let ages = getAges(people);
(with good ol' javascript)
let fgh = (x) => { f(g(h(x))); }; fgx(x);
let fgh = R.compose(f,g,h); fgh(x);
(pro tip - read from right to left, bottom to top)
Bonus points for being "points-free"
let add3Things = (a, b, c) => a + b + c ; let six = add3Things(1, 2, 3); let curriedAdd3Things = R.curry(add3Things); let six = curriedAdd3Things(1)(2)(3); // or let onePlusTwoMoreThings = curriedAdd3Things(1); let threePlusOneMoreThing = onePlusTwoMoreThings(2); let six = threePlusOneMoreThing(3);
let people = [ {name: "Joe", age: 31}, {name: "Jack", age: 32}, {name: "Jim", age: 29} ]; let getAges = R.map(R.prop('age')): let ages = getAges(people); // [31, 32, 29]
What is the oldest age?
let getOldest = R.compose(R.max, getAges); let oldest = getOldest(people); // 32
Who is under 30?
let isUnder30 = R.compose(R.gt(30), R.prop('age')); let peopleUnder30 = R.compose( R.map(R.prop('name')), R.filter(isUnder30) )(people); // ['Jim']
So what does this mean:
k => {k: v} => {k: v} => Boolean
A function that takes a key, then one object, then another object, and returns a boolean.
Aka R.eqProps - Do two objects have the same value for a given key?