Underscore.js _.iterators.accumulate() method

With the help of _.iterators.accumulate(iter, binaryFn) method, we can get the new iterator function which when called it will iterate next step with iter and generate the value by using binaryFn.

Syntax:

_.iterators.accumulate(iter, binaryFn)

Return: Return the iterator function which will generate result with accumulated Binary function.

Example 1:

In this example we can see that by using _.iterators.accumulate(iter, binaryFn) method, we are able to get the function iterator which will return the value by using accumulated binary function.



filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
var iter = _.iterators.List(["Geeks", "for", "Geeks"]);
  
function calculateLength(geeky, element) {
    return element.length;
}
  
var gfg = _.iterators.accumulate(iter, calculateLength, 0);
  
for(var i = 0; i < 3; i++) {
    console.log(gfg());
}

chevron_right


Output:

5

3

5

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
var iter = _.iterators.List(["A", "AB", "ABC", "AB", "A"]);
  
function calculateLength(geeky, element) {
    return element.length;
}
  
var gfg = _.iterators.accumulate(iter, calculateLength, 0);
  
for(var i = 0; i < 5; i++) {
    console.log(gfg());
}

chevron_right


Output:

1

2

3

2

1

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.