Skip to content

Instantly share code, notes, and snippets.

@azder
Last active October 5, 2016 13:57
Show Gist options
  • Save azder/d4d121716ece865712294958500e75fa to your computer and use it in GitHub Desktop.
Save azder/d4d121716ece865712294958500e75fa to your computer and use it in GitHub Desktop.
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!' ]
/**
* 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!' ]
@jdalton
Copy link

jdalton commented Sep 20, 2016

Here's the same snippet with lodash/fp.
(it's super similar to the R snippet)

// cherry-pick as an alternative to: _ = require('lodash/fp');
// in ES6 you can do: import { compose, filter, map } from 'lodash/fp';
const compose = require('lodash/fp/compose');
const filter = require('lodash/fp/filter');
const map = require('lodash/fp/map');

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