柯理化与反柯理化

<script>
function sum(a,b,c,d,e){
return a+b+c+d+e
}
const currying = (fn,arg = [])=>(...args)=>(
fn.length === args.concat(arg).length?fn(...args.concat(arg))
:currying(fn,[...args,...arg])
)

const fn = currying(sum)
console.log(fn(1,2)(3)(4)(5))
const fn1 = fn(1)
const fn2 = fn1(2)
console.log(fn2(3,4)(5))
</script>
 
 
<script>
Function.prototype.uncurring = function(){
let self = this
return function (){
let obj = Array.prototype.shift.call(arguments)
return self.apply(obj,arguments)
}
}
let push = Array.prototype.push.uncurring()
let cu = {}
push(cu,"sex","shuai")
console.log(cu)

</script>
posted @ 2021-06-30 10:05  国服第一李师师  阅读(41)  评论(0编辑  收藏  举报