preface

You may want to know about Currization, but you hear a lot about it. When do you use it?

I don’t know. I’ll forget.

This article will give you a brief overview of the concepts and more practical applications.

What is Corrification?

Curry is a concept of functional programming, as wikipedia puts it:

currying is the technique of converting a function that takes multiple arguments into a sequence of functions that each takes a single argument

Currization is the technique of converting a function that takes multiple arguments to a sequence of functions that takes only one argument.

???? At a loss. I’m going to show you an example of what a Currization is.

example

Add curry

The simplest example:

// Function add(x, y) {return x + y} const add = x => y => x + y add(1)(2) . args) => args.length >= fn.length ? fn(... args) : (... _args) => curry(fn, ... args, ... _args) const baseAdd = (x, y, z) => x + y + z const add = curry(baseAdd) console.log(add(1, 2, 3)); // 6 console.log(add(1)(2)(3)); // 6 console.log(add(1, 2)(3)); // 6 console.log(add(1)(2, 3)); / / 6Copy the code

Feasible? Is feasible. Useful? Interviews are useful.

If you’re looking for an interview, that’s all you need to see. If you want to use it in a practical way, keep reading.

Type determination cremation

/ / no curry function isArray (v) {return Object. The prototype. ToString. Call (v) slice (8, 1) = = = "Array"; } function isObject(v) { return Object.prototype.toString.call(v).slice(8, -1) === "Object"; } function isString(v) { return Object.prototype.toString.call(v).slice(8, -1) === "String"; } / / curry function getType (type) {return function (v) {return Object. The prototype. ToString. Call (v) slice (8, 1) = = = the type; }; } const isArray = getType("Array"); const isObject = getType("Object"); const isString = getType("String");Copy the code

Let’s start by writing a getType function that passes in a type and returns a function that determines what type it is. The isString function is then implemented using isString = getType(“String”).

As you can see, there are fewer lines of code, and even fewer as you add more functions; Another advantage is that we can optimize the judgment logic so that we can optimize only one sentence in the Corrification without changing N judgment functions.

conclusion

In the first example, we can see how it works, but we don’t know why it works, while in the second example, we can see how it works in practice.

Why are so many examples completely off the mark? I think there are two kinds: one is for the lecture programming, one is for the interview programming.

Currization is the technique of converting a function that takes multiple arguments into a sequence of functions that takes only one argument.

That’s the theory, but if we use it this way:

function getBool(trueFlag , falseFlag) {
  return function(v) {
    if (v === trueFlag) return true
    else if (v === falseFlag) return false
    else return null
  } 
}

export const stringToBool = getBool('true', 'false')
export const oneOrZeroToBool = getBool(1, 0)
export const oneOrZeroStrToBool = getBool('1', '0')
Copy the code

It actually takes two arguments, so is it corrified? If not, what is it?

In my eyes, it is corrification. Better to believe in books than to have no books at all