[#27 compose 函数]的递归解法


  • 0

    回复: #27 compose 函数

    const compose = (...args) => {
        /* TODO */
        if (!args.length) return num => num
        return num => args[0](compose(...args.slice(1, args.length))(num))
    }
    
    //test
    
    function A(num) {
        return 2 * num
      }
    
      function B(num) {
        return 3 + num
      }
    
      function C(num) {
        return 4 * num
      }
    
    console.log(compose(A, B, C)(1))
    console.log(compose(A, B)(1))
    console.log(compose(A)(1))
    console.log(compose()(1))
    
    // output:
    // => 14
    // => 8
    // => 2
    // => 1
    

登录后回复
 

与 ScriptOJ 的连接断开,我们正在尝试重连,请耐心等待