thunk

Generate a thunk.

Usage

var thunk = require( '@stdlib/utils/thunk' );

thunk( fcn[, ...args] )

Returns a thunk (i.e., an anonymous function having arity 0 and which invokes a provided function with specified arguments).

var add = require( '@stdlib/math/base/ops/add' );

var f = thunk( add, 2.0, 3.0 );
// returns <Function>

var v = f();
// returns 5

// ...

v = f();
// returns 5

Examples

var add = require( '@stdlib/math/base/ops/add' );
var decorateAfter = require( '@stdlib/utils/decorate-after' );
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
var thunk = require( '@stdlib/utils/thunk' );

function log( v ) {
    console.log( v );
}

// Create a PRNG for generating uniformly distributed pseudorandom integers:
var randi = discreteUniform( 100, 1000 );

// Randomly delay evaluation of various thunks...
var i;
for ( i = 0; i < 10; i++ ) {
    setTimeout( decorateAfter( thunk( add, i, i+1 ), 0, log ), randi() );
}
Did you find this page helpful?