1.test

函数柯里化

    function curry(fn){
        console.log('fn',fn)
        console.log('arguments',arguments.length)
      console.log('fn.length',fn.length)
        if(fn.length<=1) return fn

        const generator=(...args)=>{
           if(fn.length===args.length){
            return fn(...args)
           }else {
               return (...args2)=>{
                   return generator(...args,...args2)
               }
           }
        }
        return generator
    }
    let add=(a,b,c,d)=>a+b+c+d;
    const curriedAdd=curry(add);
    let n=curriedAdd(5)(6)(7)(8)
var currying = function(fn) {
  var args = Array.prototype.slice.call(arguments, 1);

  return function() {
    if (arguments.length === 0) {
      return fn.apply(this, args); // 没传参数时,调用这个函数
    } else {
      [].push.apply(args, arguments); // 传入了参数,把参数保存下来
      return arguments.callee; // 返回这个函数的引用
    }
  }
}


var cost = (function() {
  var money = 0;
  return function() {
    for (var i = 0; i < arguments.length; i++) {
      money += arguments[i];
    }
    return money;
  }
})();

var cost = currying(cost, 100);
cost(200); // 传入了参数,不真正求值
cost(300); // 传入了参数,不真正求值

console.log(cost()); // 求值并且输出600
重要  递归遍历树 来源 https://juejin.cn/post/6983904373508145189#heading-7
    let arr = [
    {id: 1, name: '部门1', pid: 0},
    {id: 2, name: '部门2', pid: 1},
    {id: 3, name: '部门3', pid: 1},
    {id: 4, name: '部门4', pid: 3},
    {id: 5, name: '部门5', pid: 4},
    {id: 11, name: '部门10000000000', pid: 5},
    {id: 12, name: '部门1000000000000000000', pid: 11},
]
// function tree(arr, pid = 0) {
//  return arr.filter(_ => _.pid == pid).map(_ => ({..._, children: tree(arr, _.id)}))
// }
//递归树
function tree(arr,pid=0){
  return  arr.filter(_=>_.pid==pid).map(_ => ({..._, children: tree(arr, _.id)}))
}


//函数组合  compose

function compose(...funcs) {
  if (funcs.length === 0) {
    return arg => arg;
  }

  if (funcs.length === 1) {
    return funcs[0];
  }

  return funcs.reduce((a, b) => (...args) => a(b(...args)));
}