Skip to content

Instantly share code, notes, and snippets.

@ikarasz
Created July 14, 2019 13:03
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 ikarasz/21f2c052ce78f8b86299544f6e73fe68 to your computer and use it in GitHub Desktop.
Save ikarasz/21f2c052ce78f8b86299544f6e73fe68 to your computer and use it in GitHub Desktop.
const dates = [
'2018-06-10',
'2013-03-21',
'2010-08-03',
'2016-02-05',
'2019-06-17',
];
const toDate = d => new Date(d);
const extractYear = d => d.getFullYear();
const isEven = n => n % 2 === 0;
function composeReducer(...fn) {
return function(reducer) {
return fn
.reduce((composition, transform) => transform(composition), reducer);
}
}
function mapper(transform) {
return function(reducer) {
return (xs, x) => reducer(xs, transform(x));
}
}
function filter(predicate) {
return function(reducer) {
return (xs, x) => predicate(x) ? reducer(xs, x) : xs;
}
}
const evenYearsReducer = composeReducer(
filter(isEven),
mapper(extractYear),
mapper(toDate)
);
const evenYears = dates.reduce(evenYearsReducer((xs, x) => xs.concat(x)), []);
console.log(evenYears);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment