Open In App

Underscore.js _.iterators.unfold() Method

Last Updated : 31 Aug, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

With the help of _.iterators.unfold() method, we can get the values from iteration function where unary function is expected to return single value whenever function is invoked by using this method.

Syntax:

_.iterators.unfold( seed, unaryFn )

Return Value: It returns the value from iteration function.

Note: To execute the below examples, you have to install the underscore-contrib library by using the following command.

npm install underscore-contrib

Example 1: In this example, we can see that by using _.iterators.unfold() method, we are able to get the value from iteration function where unary function return only single value whenever function is invoked.

Javascript




// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
function isGFG (val) {
    return val + " for Geeks";
}
  
var geek = _.iterators.unfold("Geeks", isGFG);
  
for(var i = 0; i < 3; i++) {
    console.log(geek());
}


Output:

Geeks
Geeks for Geeks
Geeks for Geeks for Geeks

Example 2:

Javascript




// Defining underscore contrib variable 
var _ = require('underscore-contrib');
  
function plusFive (val) {
    return val + 5;
}
  
var geek = _.iterators.unfold(1, plusFive);
  
for(var i = 0; i < 3; i++) {
    console.log(geek());
}


Output:

1
6
11

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads