HomeLanguagesJavascriptUnderscore.js _.iterators.unfold() Method

Underscore.js _.iterators.unfold() Method

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
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32548 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6922 POSTS0 COMMENTS
Nicole Veronica
12039 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12144 POSTS0 COMMENTS
Shaida Kate Naidoo
7059 POSTS0 COMMENTS
Ted Musemwa
7300 POSTS0 COMMENTS
Thapelo Manthata
7016 POSTS0 COMMENTS
Umr Jansen
7005 POSTS0 COMMENTS