currying

Also known as partial evaluation, a tree of functions that can be curried. If the parameter length is smaller than the parameter length, the function will not be executed immediately, but will return a new function. Currying is very important in FP(functional programming)

ES6 implementation of Corrification

function currying (callback, ... initialParam) {
    return (. param) = > {
        return ((params) = > {
            returnparams.length === callback.length ? callback(... params) : currying(callback, ... params) })([...initialParam, ...param]) } }Copy the code

ES5 implementation of Corrification

function currying () {
    let initialParams = Array.prototype.slice.call(arguments)
    let callback = params.shift()
    return function () {
        return (function (params) {
            returnparams.length === callback.length ? callback(... params) : currying(callback, ... params) })(initialParams.concat(Array.prototype.slice.call(arguments)))}}Copy the code

The use of corrification

function fn (a, b, c) {
    console.log(a + b + c)
}

let fn1 = currying(fn, 1)

let fn2 = fn1(2)

fn2(3) / / 6
Copy the code

uncurrying

Using call, apply to the Array to borrow some other types of functions, such as, Array. Prototype. Push. Call, Array. Prototype. Slice. Call, uncrrying to generalize these methods, not only just for Array, Better semantics.

The realization of anti-Currization

Function.prototype.uncurrying = function () {
    let self = this
    return function () {
        let obj = Array.prototype.shift.call(arguments)
        return self.apply(obj, arguments)}}Copy the code
// Push can take arguments that are not arrays
let push = Array.prototype.push.uncurrying()

(function () {
    push(arguments.4)
})(1.2.3)
Copy the code