About ES6 Arrow functions

Source: Internet
Author: User

Transfer from HTTP://SIMPLYY.SPACE/ARTICLE/577C5B0DCBE0A3E656C87C24
Multiple successive arrow functions and the high-order function of the curry

Higher-order function definition: a function that functions as an argument or a return value is a function.

So there are two kinds of higher-order functions:

    1. are common to us sort , reduce such as functions.
    2. The return value is a function of the function.

Generally speaking, it is easy to understand the common higher order functions. Like what:

function add(a) {    return function(b) { return a + b }}var add3 = add(3)add3(4) === 3 + 4 //true

The Add function is equivalent to the notation in ES6.

a => b => a + b

In fact, the above is the function of the curry is only written with ES6, changed a look, the following detailed introduction of its principles and characteristics.

Multiple consecutive arrow functions

But when a bunch of arrow functions in front of you, you will not have a hint of hesitation, I was before this is a face of the crazy ... For example, I see the following redux-thunk source (yes, the entire source code only 14 lines) in the time of multiple consecutive arrow functions ...

// 形如a => b => c => {xxx}

How easy it is to understand what these arrows did, and when I looked at the curry, I understood it instantly,

Multiple successive arrow functions are the way of ES6 's multiple-curry notation.

Currying

Let's take a look at StackOverflow's highest-ticket answer on how to understand multiple arrow functions,

He says this is the function of the curry. (This is a curried function)

A wiki's curry definition: a technique that transforms a function that takes multiple arguments into a function that takes a single parameter, and returns a new function that takes the remaining parameters and returns the result.

Well, now you know, I'll just say it briefly.

The key is to understand the curry, in fact, it can be understood that, after the first parameter variable exists in the function (closure), and then the function that needs n parameters can be changed to just the remaining (n-1) parameters can be called, such as

let add = x => y => x + ylet add2 = add(2)

Should have finished add this operation, should be

(x, y) => x + y

It takes two arguments, and now the ADD2 function requires only one parameter for the same operation, which is widely used in functional programming.

To explain in detail, the ADD2 function is equivalent to a function with a closure variable of x y => x + y

And at this point x = 2, so this call

add2(3) === 2 + 3
Back to the Chase
let add = x => y => x + y

The Add function can be understood as a wiki-only one time, so what is the following?

a => b => c => {xxx}

The function of n consecutive arrows is actually the n-1 times of the curry.

The specific invocation process is as follows:

The first n-1 call, in fact, is to pass parameters in advance, and not call the inner function body, the last call will call the inner function body, and return the most inner function body return value.

In conjunction with the above, several successive arrows (whether three or more of the two arrow functions) are linked together to curry.

So the continuous arrow function is the ES6 notation of the multiple-curry function.

let test = a => b => c => {xxx}
Invocation Features
let test = a => b => c => {xxx}

For example, for the above test function, it has 3 arrows, this function to be called 3 times test(a)(b)(c) , the first two calls just pass parameters, only the last call will return the {xxx} code snippet return value, and in the {xxx} code snippet can call A,b,c

Functions of the Curry function
    1. can be lazy evaluated
    2. Partial parameters can be passed in advance

About ES6 Arrow functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.