@stdlib/utils-if-then
v0.2.2
Published
If a condition is truthy, invoke `x`; otherwise, invoke `y`.
Downloads
11
Readme
ifthen
If a condition is truthy, invoke
x
; otherwise, invokey
.
Installation
npm install @stdlib/utils-if-then
Usage
var ifthen = require( '@stdlib/utils-if-then' );
ifthen( bool, x, y )
If a condition is truthy, invokes x
; otherwise, invokes y
.
function x() {
return 1.0;
}
function y() {
return -1.0;
}
var z = ifthen( true, x, y );
// returns 1.0
z = ifthen( false, x, y );
// returns -1.0
Notes
- The function is similar to
ifelse()
, but allows deferred argument evaluation.
Examples
var randu = require( '@stdlib/random-base-randu' );
var ceil = require( '@stdlib/math-base-special-ceil' );
var repeatString = require( '@stdlib/string-repeat' );
var ifthen = require( '@stdlib/utils-if-then' );
var z;
var i;
function x() {
return repeatString( 'BOOP', ceil( randu()*3.0 ) );
}
function y() {
return repeatString( 'beep', ceil( randu()*5.0 ) );
}
for ( i = 0; i < 100; i++ ) {
z = ifthen( randu() > 0.9, x, y );
console.log( z );
}
See Also
@stdlib/utils-async/if-then
: if a predicate function returns a truthy value, invokex
; otherwise, invokey
.@stdlib/utils-if-else
: if a condition is truthy, returnx
; otherwise, returny
.
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.