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