JavaScript-関数の組み合わせ
1124 ワード
const _ = require("lodash");
const reverse = arr => arr.reverse()
const first = arr => arr[0]
const toUpper = s => s.toUpperCase()
const lastToupper = _.flowRight(toUpper, first, reverse)
console.log(lastToupper(['one', 'two', 'three']))
// lodash flowRight
function compose (...args) {
return function (value) {
return args.reverse().reduce(function (acc, fn) {
console.log(fn)
return fn(acc)
}, value)
}
}
const composeEs6 = (...args) => value => args.reverse().reduce((acc, fn) => fn(acc), value)
const f = composeEs6(toUpper, first, reverse)
console.log(f(['one', 'two', 'three']))
//
原文の住所:https://kspf.xyz/archives/71