Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is double arrow function?

What is "let x= something1 => something2 => something3" ?

I have this code and I'm failing to understand what does it do.

const myReducers = {person, hoursWorked};
const combineReducers = reducers => (state = {}, action) => {
  return Object.keys(reducers).reduce((nextState, key) => {
    nextState[key] = reducers[key](state[key], action);
    return nextState;
  }, {});
};

The full code incase you need:

//Redux-Style Reducer
const person = (state = {}, action) => {
  switch(action.type){
    case 'ADD_INFO':
      return Object.assign({}, state, action.payload)
    default:
      return state;
  }
}

const infoAction = {type: 'ADD_INFO', payload: {name: 'Brian', framework: 'Angular'}}
const anotherPersonInfo = person(undefined, infoAction);
console.log('***REDUX STYLE PERSON***: ', anotherPersonInfo);

//Add another reducer
const hoursWorked = (state = 0, action) => {
  switch(action.type){
    case 'ADD_HOUR':
      return state + 1;
    case 'SUBTRACT_HOUR':
      return state - 1;
    default:
      return state;
  }
}
//Combine Reducers Refresher

****HERE****
****HERE****
****HERE****

const myReducers = {person, hoursWorked};
const combineReducers = reducers => (state = {}, action) => {
  return Object.keys(reducers).reduce((nextState, key) => {
    nextState[key] = reducers[key](state[key], action);
    return nextState;
  }, {});
};


****
****


/*
This gets us most of the way there, but really want we want is for the value of firstState and secondState to accumulate
as actions are dispatched over time. Luckily, RxJS offers the perfect operator for this scenario., to be discussed in next lesson.
*/
const rootReducer = combineReducers(myReducers);
const firstState = rootReducer(undefined, {type: 'ADD_INFO', payload: {name: 'Brian'}});
const secondState = rootReducer({hoursWorked: 10, person: {name: 'Joe'}}, {type: 'ADD_HOUR'});
console.log('***FIRST STATE***:', firstState);
console.log('***SECOND STATE***:', secondState);

From: https://gist.github.com/btroncone/a6e4347326749f938510

like image 516
Stav Alfi Avatar asked Jul 23 '16 11:07

Stav Alfi


2 Answers

let x= something1 => something2 => something3 is almost same as the following:

let x = function (something) {
  return function (something2) {
    return something3
  }
}

The only difference is, arrows have lexical binding of this, i.e. binding in compile time.

like image 91
Aakash Sigdel Avatar answered Nov 12 '22 11:11

Aakash Sigdel


An arrow function is

someParameters => someExpression

So, what is

someParameters => someThing => someThingElse

???

Well, by simple stupid "pattern matching", it's an arrow function, whose body (someExpression) is

someThing => someThingElse

In other words, it's

someParameters => someOtherParameters => someExpression

There's nothing special about this. Functions are objects, they can be returned from functions, no matter if those functions are written using arrows or the function keyword.

The only thing you really need to know to read this properly is that the arrow is right-associative, IOW that

a => b => c === a => (b => c)

Note: An arrow function can also have a body consisting of statements as well as a single expression. I was specifically referring to the form the OP is confused about.

like image 39
Jörg W Mittag Avatar answered Nov 12 '22 11:11

Jörg W Mittag