Skip to content

Commit e446bba

Browse files
committed
Add functional if-then utility
1 parent 08d8747 commit e446bba

File tree

8 files changed

+460
-0
lines changed

8 files changed

+460
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# ifthen
2+
3+
> If a condition is truthy, invoke `x`; otherwise, invoke `y`.
4+
5+
6+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
7+
8+
<section class="intro">
9+
10+
</section>
11+
12+
<!-- /.intro -->
13+
14+
<!-- Package usage documentation. -->
15+
16+
<section class="usage">
17+
18+
## Usage
19+
20+
``` javascript
21+
var ifthen = require( '@stdlib/utils/if-then' );
22+
```
23+
24+
#### ifthen( bool, x, y )
25+
26+
If a condition is truthy, invokes `x`; otherwise, invokes `y`.
27+
28+
``` javascript
29+
function x() {
30+
return 1.0;
31+
}
32+
33+
function y() {
34+
return -1.0;
35+
}
36+
37+
var z = ifthen( true, x, y );
38+
// returns 1.0
39+
40+
z = ifthen( false, x, y );
41+
// returns -1.0
42+
```
43+
44+
</section>
45+
46+
<!-- /.usage -->
47+
48+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
49+
50+
<section class="notes">
51+
52+
## Notes
53+
54+
* The function is similar to [`ifelse()`][@stdlib/utils/if-else], but allows deferred argument evaluation.
55+
56+
</section>
57+
58+
<!-- /.notes -->
59+
60+
<!-- Package usage examples. -->
61+
62+
<section class="examples">
63+
64+
## Examples
65+
66+
``` javascript
67+
var randu = require( '@stdlib/math/base/random/randu' );
68+
var ifthen = require( '@stdlib/utils/if-then' );
69+
70+
var z;
71+
var i;
72+
73+
function x() {
74+
return repeatString( 'BOOP', ceil( randu()*3.0 ) );
75+
}
76+
77+
function y() {
78+
return repeatString( 'beep', ceil( randu()*5.0 ) );
79+
}
80+
81+
for ( i = 0; i < 100; i++ ) {
82+
z = ifthen( randu() > 0.9, x, y );
83+
console.log( z );
84+
}
85+
```
86+
87+
</section>
88+
89+
<!-- /.examples -->
90+
91+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
92+
93+
<section class="references">
94+
95+
</section>
96+
97+
<!-- /.references -->
98+
99+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
100+
101+
<section class="links">
102+
103+
[@stdlib/utils/if-else]: https://github.com/stdlib-js/stdlib
104+
105+
</section>
106+
107+
<!-- /.links -->
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var randu = require( '@stdlib/math/base/random/randu' );
7+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
8+
var pkg = require( './../package.json' ).name;
9+
var ifthen = require( './../lib' );
10+
11+
12+
// MAIN //
13+
14+
bench( pkg, function benchmark( b ) {
15+
var z;
16+
var i;
17+
18+
function x() {
19+
return randu() * 100.0;
20+
}
21+
22+
function y() {
23+
return -1.0 * randu() * 100.0;
24+
}
25+
26+
b.tic();
27+
for ( i = 0; i < b.iterations; i++ ) {
28+
z = ifthen( randu() > 0.5, x, y );
29+
if ( isnan( z ) ) {
30+
b.fail( 'should not return NaN' );
31+
}
32+
}
33+
b.toc();
34+
if ( isnan( z ) ) {
35+
b.fail( 'should not return NaN' );
36+
}
37+
b.pass( 'benchmark finished' );
38+
b.end();
39+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
{{alias}}( bool, x, y )
3+
If a condition is truthy, invoke `x`; otherwise, invoke `y`.
4+
5+
Parameters
6+
----------
7+
bool: boolean
8+
Condition.
9+
10+
x: Function
11+
Function to invoke if a condition is truthy.
12+
13+
y: Function
14+
Function to invoke if a condition is falsy.
15+
16+
Returns
17+
-------
18+
z: any
19+
Return value of either `x` or `y`.
20+
21+
Examples
22+
--------
23+
> function x() { return 'yes'; };
24+
> function y() { return 'no'; };
25+
> var z = {{alias}}( true, x, y )
26+
'yes'
27+
> z = {{alias}}( false, x, y )
28+
'no'
29+
30+
See Also
31+
--------
32+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'use strict';
2+
3+
var randu = require( '@stdlib/math/base/random/randu' );
4+
var ceil = require( '@stdlib/math/base/special/ceil' );
5+
var repeatString = require( '@stdlib/string/repeat' );
6+
var ifthen = require( './../lib' );
7+
8+
var z;
9+
var i;
10+
11+
function x() {
12+
return repeatString( 'BOOP', ceil( randu()*3.0 ) );
13+
}
14+
15+
function y() {
16+
return repeatString( 'beep', ceil( randu()*5.0 ) );
17+
}
18+
19+
for ( i = 0; i < 100; i++ ) {
20+
z = ifthen( randu() > 0.9, x, y );
21+
console.log( z );
22+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var isFunction = require( '@stdlib/assert/is-function' );
6+
7+
8+
// MAIN //
9+
10+
/**
11+
* If a condition is truthy, invokes `x`; otherwise, invokes `y`.
12+
*
13+
* @param {boolean} bool - condition
14+
* @param {Function} x - function to invoke if a condition is truthy
15+
* @param {Function} y - function to invoke if a condition is falsy
16+
* @throws {TypeError} second argument must be a function
17+
* @throws {TypeError} third argument must be a function
18+
* @returns {*} return value of either `x` or `y`
19+
*
20+
* @example
21+
* var randu = require( '@stdlib/math/base/random/randu' );
22+
*
23+
* function x() {
24+
* return randu() * 100.0;
25+
* }
26+
*
27+
* function y() {
28+
* return -1.0 * randu() * 100.0;
29+
* }
30+
*
31+
* var z = ifthen( randu() > 0.5, x, y );
32+
* // returns <number>
33+
*/
34+
function ifthen( bool, x, y ) {
35+
if ( !isFunction( x ) ) {
36+
throw new TypeError( 'invalid input argument. Second argument must be a function. Value: `'+x+'`.' );
37+
}
38+
if ( !isFunction( y ) ) {
39+
throw new TypeError( 'invalid input argument. Third argument must be a function. Value: `'+y+'`.' );
40+
}
41+
if ( bool ) {
42+
return x();
43+
}
44+
return y();
45+
} // end FUNCTION ifthen()
46+
47+
48+
// EXPORTS //
49+
50+
module.exports = ifthen;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
/**
4+
* If a condition is truthy, invoke `x`; otherwise, invoke `y`.
5+
*
6+
* @module @stdlib/utils/if-then
7+
*
8+
* @example
9+
* var randu = require( '@stdlib/math/base/random/randu' );
10+
* var ifthen = require( '@stdlib/utils/if-then' );
11+
*
12+
* function x() {
13+
* return randu() * 100.0;
14+
* }
15+
*
16+
* function y() {
17+
* return -1.0 * randu() * 100.0;
18+
* }
19+
*
20+
* var z = ifthen( randu() > 0.5, x, y );
21+
* // returns <number>
22+
*/
23+
24+
// MODULES //
25+
26+
var ifthen = require( './if_then.js' );
27+
28+
29+
// EXPORTS //
30+
31+
module.exports = ifthen;
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"name": "@stdlib/utils/if-then",
3+
"version": "0.0.0",
4+
"description": "If a condition is truthy, invoke `x`; otherwise, invoke `y`.",
5+
"author": {
6+
"name": "The Stdlib Authors",
7+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
8+
},
9+
"contributors": [
10+
{
11+
"name": "The Stdlib Authors",
12+
"url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
13+
}
14+
],
15+
"scripts": {},
16+
"main": "./lib",
17+
"repository": {
18+
"type": "git",
19+
"url": "git://github.com/stdlib-js/stdlib.git"
20+
},
21+
"homepage": "https://github.com/stdlib-js/stdlib",
22+
"keywords": [
23+
"stdlib",
24+
"stdutils",
25+
"stdutil",
26+
"utilities",
27+
"utility",
28+
"utils",
29+
"util",
30+
"if",
31+
"else",
32+
"then",
33+
"if-else",
34+
"if-then",
35+
"defer",
36+
"deferred",
37+
"delay",
38+
"delayed",
39+
"control",
40+
"flow",
41+
"condition",
42+
"conditional",
43+
"either-or"
44+
],
45+
"bugs": {
46+
"url": "https://github.com/stdlib-js/stdlib/issues"
47+
},
48+
"dependencies": {},
49+
"devDependencies": {},
50+
"engines": {
51+
"node": ">=0.10.0",
52+
"npm": ">2.7.0"
53+
},
54+
"license": "Apache-2.0"
55+
}

0 commit comments

Comments
 (0)