柯里化函数

const curring = fn => {
     const { length } = fn
     const curried = (...args) => {
         return (args.length >= length
               ? fn(...args)
               : (...args2) => curried(...args.concat(args2)))
     }
     return curried
 }
 
 const listMerge = (a, b, c) => [a, b, c]
 const curried = curring(listMerge)
 console.log(curried(1)(2)(3)) // [1, 2, 3]
 
 console.log(curried(1, 2)(3)) // [1, 2, 3]
 
 console.log(curried(1, 2, 3)) // [1, 2, 3]

  

posted @ 2020-12-18 14:49  LeoX的爬坑笔记  阅读(71)  评论(0编辑  收藏  举报