Example on how to create own compose function and how to use it. Please notice that the function3 parameter was applied first? This is very important. Functions are applied from right to left.
A Pen by Vlad Bezden on CodePen.
Example on how to create own compose function and how to use it. Please notice that the function3 parameter was applied first? This is very important. Functions are applied from right to left.
A Pen by Vlad Bezden on CodePen.
'use strict' | |
Function.prototype.compose = function(prevFunc) { | |
const nextFunc = this; | |
return function() { | |
return nextFunc.call(this, prevFunc.apply(this, arguments)); | |
} | |
} | |
function function1(a) { | |
return a + ' 1'; | |
} | |
function function2(b) { | |
return b + ' 2'; | |
} | |
function function3(c) { | |
return c + ' 3'; | |
} | |
const composition = function3.compose(function2).compose(function1); | |
console.log(composition('count')); |
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/lodash/4.14.1/lodash.fp.min.js"></script> |