introduce

Kerochemistry is a kind of advanced technique about function. Currization is the transformation of a function from a callable f(a, b, c) to a callable F (a)(b)(c). Currization does not call a function. It just transforms the function. The simplest example

Function sum(a, b) {return a + b; Function curry_sum(a) {return function(b){return a+b}} sum(1)(2) // 3 // or fn=sum(1) fn(2) ==>3Copy the code

Why do you want to chemistry

The use of chemical chemistry is certainly not for loading force, or not just for loading force.

Keratology can delay the execution to achieve the purpose of parameter reuse. (I understand that dynamic functions and inert functions have nothing to do with keratology)

So sum(1,2) was immediately executed, so now we can do fn=sum(1) and then fn(2) and that’s going to control the execution of the function

The first pass does not execute. We ambush it first.

And then we’re going to reuse the arguments that we passed the first time, and that’s called delayed execution, parameter reuse. This will greatly simplify our code.

What does a functional coriochemical function look like

Although we can write the function that we want, like curry_sum, according to The idea of Coriolis

But it would be better to write a more generic kerochemical function that takes a fn and returns a wrapped function called curry_fn, which does all the reuse and delay we need.

Here’s what a curry function looks like with reference to Lodash

/** * argument * func: the function used to curry. * [arity=func.length] : the number of arguments that need to be supplied to func. * * Returns * (Function): Returns the new Curry Function. */ function curry(func, [arity=func.length]){ }Copy the code

We give it a test case

// curry.test.js
const{curry} = require('./index')

test('curry'.() = >{
    const add = (a,b,c) = >{
        return a+b+c
    }
    const sayhi = function(){
        return this.name
    }
    const curry_add = curry(add)
    const curry_sayhi = curry(sayhi)
    const obj = {name:'fyy'}
    expect(curry_sayhi.call(obj)).toBe('fyy')
    expect(curry_add(2) (3) (4)).toBe(9)
    expect(curry_add(1) (2.3)).toBe(6)
    expect(curry_add(1.2) (2)).toBe(5)
    expect(curry_add(1.2.1)).toBe(4)})Copy the code

implementation

We implement a curry function in index.js

There are several key points to note when implementing this

  • Use closures to store parameters
  • Fn actually executes when the number of parameters passed by fn reaches the number of parameters accepted by FN
  • We’re going to use apply, and we’re not going to use arrows here, because they don’t apply, and they’re going to point to the empty object in this,node that we defined
  • One use of recursion
//index.js const curry = function(fn){ return function curried(... If (fn.length===args. Length){return fn.apply(this,args)}else{return function(... args1){ return curried.apply(this,args.concat(args1)) } } } } module.exports = { curry }Copy the code

So let’s run the test case to prove that the function works.

A practical example of physics and chemistry

Let’s look at two examples of physics and chemistry to deepen our understanding

Encapsulates the request

Let’s say the API looks like this ajax(Method, URL,params)

// We can wrap a post in serve.js const post = url=> data => Ajax ('post',url,params) // Wrap some addresses in module1api.js import {post} From 'serve.js' const getList = post('www.xxx.com/api/xxx') Import {getList} from 'module1api.js' const data = getList(params) // Import {getList} from in the second place 'module1api.js' const data = getList(params) // NTH place... import{getList} from 'module1api.js' const data = getList(params)Copy the code

Simplified processing of data

Const data = [{name: ‘Kevin ‘}, {name:’ Daisy ‘}] we need to process it into [‘ Kevin ‘,’ Daisy ‘] every time we call the Map method here

var _data = data.map(function (item) {
    return item.name;
})
Copy the code

We could actually write it this way, and this is also using Cauchy

const prop = curry(function (key, obj) { return obj[key] }); Var data = person.map(val=>prop('name')(val)) // var data = person.map(prop('name')) Fn. Length ===args. Length = >== */Copy the code

conclusion

You don’t have to use the curry function to write beautiful and efficient code.

Corrification is, after all, an advanced application of closures.

For example, the compose function, which we will learn about in the future, is an extension of the composition method.

Due to limited technology, if you find any mistakes in reading, please point them out in the comments

If you feel that this article is of great help to you, please like it and collect it. Your support is the biggest motivation for the author to write!

Refer to the article

Ke the physical and chemical

Currization of JavaScript topics

lodash_curry

Currization of applications of higher order functions

Segmentfault.com/a/119000001…