@stdlib/utils-thunk
v0.2.2
Published
Generate a thunk.
Downloads
7
Readme
thunk
Generate a thunk.
Installation
npm install @stdlib/utils-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() );
}
Notice
This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.
For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.
Community
License
See LICENSE.
Copyright
Copyright © 2016-2024. The Stdlib Authors.