1

This code is used for finding the cumulative sum of an array [5, 10, 3, 3] i.e.

output = [5,15,18,21]

here everything is correct i just want to know the meaning of this line

(sum => value => sum += value)(0)

anybody who knows this ?

const cumulativeSum = (sum => value => sum += value)(0);

console.log([5, 10, 3, 2].map(cumulativeSum));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • 2
    Does it make more sense as `sumFn = function (sum) { return function(value) { return sum += value } }; cumulativeSum = sumFn(0)`? – Phil Aug 10 '22 at 06:32
  • Are you asking what [Arrow functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions) are? – CBroe Aug 10 '22 at 06:34
  • [Map with a callback function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map#callbackfn) – mplungjan Aug 10 '22 at 06:38
  • @CBroe yes i want to know the meaning of these , i.e. if we are writing this way (sum => value => sum += value)(0), then what does that mean – Karishma Singh Aug 10 '22 at 06:40
  • This question shouldn't be closed. The linked question isn't relevant to what OP's asking about. – Brother58697 Aug 10 '22 at 06:42
  • He's not asking about arrow functions or calling a function without parameters, he's asking about the way it was curried – Brother58697 Aug 10 '22 at 06:43
  • @Brother58697 yes you are right i am not asking about arrow functions , i am asking about the way it is carried furthur – Karishma Singh Aug 10 '22 at 06:51
  • @KarishmaSingh If the linked answer isn't enough, here's a [jsbin](https://jsbin.com/wirunox/edit?js,console) explaining it with an example. – Brother58697 Aug 10 '22 at 07:03
  • 1
    @Brother58697 the question is re-opened. you can post your jsbin but do rename c u m to cumulative or cumul to not be flagged by search engines – mplungjan Aug 10 '22 at 07:05
  • @mplungjan Will do, and fair enough haha! :P – Brother58697 Aug 10 '22 at 07:06
  • @mplungjan it's still closed on my end – Brother58697 Aug 10 '22 at 07:06
  • Shift-reload the browser – mplungjan Aug 10 '22 at 07:22
  • I tried, but it's still taking me either here with the `noredirect` flag which shows it's closed, or auto ridirects to 'What is Currying', even if I try to access it from @KarishmaSingh's account page – Brother58697 Aug 10 '22 at 07:28

0 Answers0