π Posted on 2020-07-02T19:27:43Z
β³ 1 min read
Simple js compose
#javascript#functional#compose
The concept of
compose
is simple β it combines n functions. Itβs a pipe flowing right-to-left, calling each function with the output of the last one.Array.prototype.reduceRight = function (...args) { const _this = this return _this.reverse().reduce(...args) } const compose = (...args) => x => args.reduceRight((acc, currFn)=>{ return currFn(acc) }, x) const double = x => x * 2 const inc = x => x + 1 const incAndOct = compose(double, double, double, inc) incAndOct(2) //24
Explanation:
we are passing
2
to incAndOct
function. First it will call inc
method then the result will be 3
and next it will apply double method on 3
so result is 6
again one more double but this time on double(3)
i.e 6
result is 12 now final double on 12
it is 24