Last active
October 5, 2016 13:57
-
-
Save azder/d4d121716ece865712294958500e75fa to your computer and use it in GitHub Desktop.
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 R = require('ramda'); | |
const drinkers = R.compose( | |
R.map(({name, surname}) => `${name} ${surname} can drink!`), | |
R.filter(({age}) => 21 < age) | |
); | |
const users = [ | |
{ | |
name: 'Kristijan', | |
surname: 'Ristovski', | |
age: 17 | |
}, | |
{ | |
name: 'Vincent', | |
surname: 'van Gogh', | |
age: 37 | |
}, | |
{ | |
name: 'Goran', | |
surname: 'Peoski', | |
age: 69 | |
} | |
]; | |
console.log(drinkers(users)); | |
//result: [ 'Vincent van Gogh can drink!', 'Goran Peoski can drink!' ] |
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
/** | |
* Created by azder on 2016-09-20. | |
*/ | |
'use strict'; // ALWAYS | |
const curry = ( | |
(fn, ...args) => args.length >= fn.length ? fn(...args) : curry.bind(null, fn, ...args) | |
); | |
const map = curry( | |
(fn, array) => array.map(fn) | |
); | |
const filter = curry( | |
(test, array) => array.filter(test) | |
); | |
const compose = ( | |
(...args) => ( | |
arg => args.reduceRight((x, fn) => fn(x), arg) | |
) | |
); | |
const drinkers = compose( | |
map(({name, surname}) => `${name} ${surname} can drink!`), | |
filter(({age}) => 21 < age) | |
); | |
const users = [ | |
{ | |
name: 'Kristijan', | |
surname: 'Ristovski', | |
age: 17 | |
}, | |
{ | |
name: 'Vincent', | |
surname: 'van Gogh', | |
age: 37 | |
}, | |
{ | |
name: 'Goran', | |
surname: 'Peoski', | |
age: 69 | |
} | |
]; | |
console.log(drinkers(users)); | |
//result: [ 'Vincent van Gogh can drink!', 'Goran Peoski can drink!' ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here's the same snippet with lodash/fp.
(it's super similar to the R snippet)