Reputation: 551
Using normal lodash without fp, you'd do something like
chain(array).map(..).reduce(...).value()
With fp, you'd do
compose(reduce(...), map(...))(array)
I can make it work for many methods (flatten, sort, map), except reduce.
You'd expect it (lodash/fp/reduce) to work like
reduce((a,b)=>a+b, 0)([1,2,3])
But the fp version still requires 3 arguments, which doesn't make sense to me. All the other functions work like this for me, except reduce
func(...)(array)
How can I make fpreduce work like other fp functions in this manner:
compose(reduce(...), map(...), flatten(...))(array)
Upvotes: 3
Views: 4174
Reputation: 135217
reduce takes 3 total arguments regardless of whether a functional interface is used. lodash/fp
just changes parameter order and allows you to partially functions
const fp = require ('lodash/fp')
const sum = fp.reduce (fp.add, 0)
const sq = x => x * x
const main = fp.compose (sum, fp.map (sq))
console.log (main ([1,2,3,4]))
// => 30
// [1,2,3,4] => [1,4,9,16] => 0 + 1 + 4 + 9 + 16 => 30
Or as an inline composition
fp.compose (fp.reduce (fp.add, 0), fp.map (x => x * x)) ([1,2,3,4])
// => 30
Upvotes: 5