Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

fp-compose

node v15.14.0
version: master
endpointsharetweet
const greeting = (firstName, lastName) => firstName + ' ' + lastName const toUpper = str => str.toUpperCase() // 声明式编程(函数式) function compose(funcs) { return funcs.reduce((a, b) => (...args) => a(b(...args))) } const fn = compose([toUpper, greeting]); const result = fn('jack', 'smith'); console.log(result);
// 命令式编程(面向过程) function compose1(fns) { let length = fns.length; let count = length - 1; let result = null; return function fn1(...args) { result = fns[count].apply(null, args); if (count <= 0) { return result } count--; return fn1(result); } } const fn1 = compose1([toUpper, greeting]); const result1 = fn1('jack', 'smith'); console.log(result);
Loading…

no comments

    sign in to comment