We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
function compose(...fns) { const length = fns.length for (let i = 0; i < length; i++) { if (typeof fns[i] !== 'function') { throw new TypeError('Expected arguments are functions') } } return function (...args) { let index = 0 let result = length ? fns[index].apply(this, args) : args while (++index < length) { result = fns[index].call(this, result) } return result } } // test function double(num) { return num * 2 } function square(num) { return num ** 2 } const newFn = compose(double, square) console.log(newFn(10))
The text was updated successfully, but these errors were encountered:
// 简单实现组合函数 const compose = (...args: any[]) => values => { return args.reverse().reduce((prev, curr) => curr(prev), values); }; const reverse = (arr: any[]) => arr.reverse(); const first = (arr: any[]) => arr[0]; const toUpper = (value: string) => value.toUpperCase(); const flowRight = compose(toUpper, first, reverse); console.log('flowRight', flowRight(['a', 'b', 'flow']));
Sorry, something went wrong.
function compose(...fns) { let res return function(...args) { fns.forEach((fn) => { if (!res) { res = fn(...args) } else { res = fn(res) } }) return res } }
/** * composeRight (参数从右往左) * compose (参数从左往右) */ export function composeRight(...fns) { return function (x) { return fns.reduceRight((v, f) => f(v), x); }; } export function compose(...fns) { return function (x) { return fns.reduce((v, f) => f(v), x); }; }
No branches or pull requests
The text was updated successfully, but these errors were encountered: