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
最近想到了一个自认为很有意思的面试题 如何实现一个 compose 函数。 函数接收数个参数,参数均为 Function 类型,右侧函数的执行结果将作为左侧函数执行的参数来调用。
compose
Function
原文
The text was updated successfully, but these errors were encountered:
1.0实现方案
大致的思路为:
所以这种情况用递归来实现会比较清晰一些
function compose (...funcs) { return function exec (arg) { let func = funcs.pop() let result = func(arg) // 执行函数,获取返回值 // 如果后续还有函数,将返回值放入下一个函数执行 // 如果后续没有了,直接返回 return funcs.length ? exec(result) : result } }
Sorry, something went wrong.
No branches or pull requests
最近想到了一个自认为很有意思的面试题
如何实现一个
compose
函数。函数接收数个参数,参数均为
Function
类型,右侧函数的执行结果将作为左侧函数执行的参数来调用。原文
The text was updated successfully, but these errors were encountered: