Created
July 14, 2019 13:03
-
-
Save ikarasz/21f2c052ce78f8b86299544f6e73fe68 to your computer and use it in GitHub Desktop.
This file contains 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 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