Underscore.js _.iterators.accumulate() method
Last Updated :
11 Apr, 2023
With the help of _.iterators.accumulate(iter, binaryFn) method, we can get the new iterator function which when called will iterate the next step with iter and generate the value by using binaryFn.
Syntax:
_.iterators.accumulate(iter, binaryFn)
Return: Return the iterator function which will generally result in an 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 the accumulated binary function.
javascript
const _ = require( 'underscore-contrib' );
let iter = _.iterators.List([ "Geeks" , "for" , "Geeks" ]);
function calculateLength(geeky, element) {
return element.length;
}
let gfg = _.iterators.accumulate(iter, calculateLength, 0);
for (let i = 0; i < 3; i++) {
console.log(gfg());
}
|
Output:
5
3
5
Example 2: In this example, we will see the use of _.iterators.accumulate(iter, binaryFn) method.
javascript
const _ = require( 'underscore-contrib' );
let iter = _.iterators.List([ "A" , "AB" , "ABC" , "AB" , "A" ]);
function calculateLength(geeky, element) {
return element.length;
}
let gfg = _.iterators.accumulate(iter, calculateLength, 0);
for (let i = 0; i < 5; i++) {
console.log(gfg());
}
|
Output:
1
2
3
2
1
Share your thoughts in the comments
Please Login to comment...