// the prime hero here function curry(func, expectedCount, ...args) { function _check(arr) { return arr.length >= expectedCount ? _result(arr) : _curry.bind(null, arr) } function _curry(prev, ...rest) { return _check(prev.concat(rest)) } function _result(arr) { return func.apply(this, arr) } return _check(args) } // 2 simple "workers" to apply currying on function multiplyAll(...arr) { return arr.reduce((p, c) => p * c, 1) } function sayTo(type, target, end = '!') { return `${type}, ${target}${end}` } // helper logger function log(...args) { console.log(args.join('\n')) } // definitions const multiply = curry(multiplyAll, 2) // uses `multiplyAll` worker, expects at least 2 arguments const double = multiply(2) // multiples by 2 const triple = multiply(3) // multiples by 3 const say = curry(sayTo, 2) // uses `sayTo` worker, expects at least 2 arguments const sayHello = say('Hello') // says "Hello, [argument]!" const sayHelloBabe = () => sayHello('babe') // says "Hello, babe!" const sayGoodBye = say('Good bye') // says "Good bye, [argument]" const sayVeryGoodBye = curry(sayTo, 2)('Very good bye') // says "Very good bye, [argument]" // running test log( multiply(1.5)(1.5), double(2), multiply(2, 3), triple(2.33), multiply(3)()(3), curry(multiplyAll, 3, 2, 3, 2), double(2, 4), multiply()(4)()(5), double(triple(4)), triple(3, 3), multiply()(3)(4, 5), curry(multiplyAll, 3)(10)(20)(30), sayHello('world'), sayHello()('cousin'), sayHelloBabe(), sayGoodBye('cruel world'), sayVeryGoodBye('for ever', '!!!'), '-= END =-' )