0

I am currently following along with a react/redux tutorial that has me using ES6 and immutable.js both of which I am new to.

In the following function:

export function vote(state, entry) {
  return state.updateIn(
    ['vote', 'tally', entry],
    0,
    tally => tally + 1
  );
}

This line: tally => tally + 1 Has me a bit confused.

Is this a feature of ES6 or immutable? I understand that the result of this function will be that the current value of tally will be incremented by 1, but the syntax is unusual to me.

In the immutable docs this line is referred to as the updater, but i don't not see a description of it and I do not understand if it is then a feature of ES6 itself. If it is whats another example of this being used and whats a non ES6 example?

I am sorry if this is a bit confusing I am not sure how else to word this question.

By the way the state map looks like this:

const state = Map({
  vote: Map({
    pair: List.of('Trainspotting', '28 Days Later'),
    tally: Map({
      'Trainspotting': 3,
      '28 Days Later': 2
    })
  }),
Andrew Font
  • 1,125
  • 2
  • 16
  • 33
  • 1
    @Gothdo: yep, that answer is much better than mine. I'll keep the link for the next time I see this question. :) – Sergio Tulentsev Oct 30 '16 at 21:58
  • Not sure if this is the same thing I know that in ES6 => can be used as a function expression "arrow notation" they call it right? But in this case tally is not a function? So tally some how being called as a function anyway? That's what's confusing me because it looks like it's calling a function called tally but I don't have a function called tally? – Andrew Font Oct 30 '16 at 22:09
  • Oh wait I think I understand now.... It's a function that's taking tally as input and return tally plus 1 .... I see now! – Andrew Font Oct 30 '16 at 22:17
  • Yep, it's an anonymous function, taking one argument (which is named `tally`) – Sergio Tulentsev Oct 30 '16 at 22:17
  • I didn't realize you didn't need the parenthesis – Andrew Font Oct 30 '16 at 22:20
  • Note that the parentheses can *only* be omitted when the arrow function has exactly one argument. – Shaggy Nov 01 '16 at 08:19

0 Answers0