Skip to content

Commit 42a2106

Browse files
committed
Add functional if-else utility
1 parent 4c4dd5c commit 42a2106

File tree

10 files changed

+436
-0
lines changed

10 files changed

+436
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# ifelse
2+
3+
> If a condition is truthy, return `x`; otherwise, return `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 ifelse = require( '@stdlib/utils/if-else' );
22+
```
23+
24+
#### ifelse( bool, x, y )
25+
26+
If a condition is truthy, returns `x`; otherwise, returns `y`.
27+
28+
``` javascript
29+
var z = ifelse( true, 'yes', 'no' );
30+
// returns 'yes'
31+
32+
z = ifelse( false, 'yes', 'no' );
33+
// returns 'no'
34+
```
35+
36+
</section>
37+
38+
<!-- /.usage -->
39+
40+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
41+
42+
<section class="notes">
43+
44+
</section>
45+
46+
<!-- /.notes -->
47+
48+
<!-- Package usage examples. -->
49+
50+
<section class="examples">
51+
52+
## Examples
53+
54+
``` javascript
55+
var randu = require( '@stdlib/math/base/random/randu' );
56+
var ifelse = require( '@stdlib/utils/if-else' );
57+
58+
var z;
59+
var i;
60+
61+
for ( i = 0; i < 100; i++ ) {
62+
z = ifelse( randu() > 0.9, 'BOOP', 'beep' );
63+
console.log( z );
64+
}
65+
```
66+
67+
</section>
68+
69+
<!-- /.examples -->
70+
71+
<!-- 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. -->
72+
73+
<section class="references">
74+
75+
</section>
76+
77+
<!-- /.references -->
78+
79+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
80+
81+
<section class="links">
82+
83+
</section>
84+
85+
<!-- /.links -->
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 ifelse = require( './../lib' );
10+
11+
12+
// MAIN //
13+
14+
bench( pkg, function benchmark( b ) {
15+
var z;
16+
var i;
17+
18+
b.tic();
19+
for ( i = 0; i < b.iterations; i++ ) {
20+
z = ifelse( randu() > 0.5, 1.0, -1.0 );
21+
if ( isnan( z ) ) {
22+
b.fail( 'should not return NaN' );
23+
}
24+
}
25+
b.toc();
26+
if ( isnan( z ) ) {
27+
b.fail( 'should not return NaN' );
28+
}
29+
b.pass( 'benchmark finished' );
30+
b.end();
31+
});
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
julia 0.5
2+
BenchmarkTools 0.0.8
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env julia
2+
3+
import BenchmarkTools
4+
5+
# Benchmark variables:
6+
name = "if-else";
7+
repeats = 3;
8+
9+
"""
10+
print_version()
11+
12+
Prints the TAP version.
13+
14+
# Examples
15+
16+
``` julia
17+
julia> print_version()
18+
```
19+
"""
20+
function print_version()
21+
@printf( "TAP version 13\n" );
22+
end
23+
24+
"""
25+
print_summary( total, passing )
26+
27+
Print the benchmark summary.
28+
29+
# Arguments
30+
31+
* `total`: total number of tests
32+
* `passing`: number of passing tests
33+
34+
# Examples
35+
36+
``` julia
37+
julia> print_summary( 3, 3 )
38+
```
39+
"""
40+
function print_summary( total, passing )
41+
@printf( "#\n" );
42+
@printf( "1..%d\n", total ); # TAP plan
43+
@printf( "# total %d\n", total );
44+
@printf( "# pass %d\n", passing );
45+
@printf( "#\n" );
46+
@printf( "# ok\n" );
47+
end
48+
49+
"""
50+
print_results( iterations, elapsed )
51+
52+
Print benchmark results.
53+
54+
# Arguments
55+
56+
* `iterations`: number of iterations
57+
* `elapsed`: elapsed time (in seconds)
58+
59+
# Examples
60+
61+
``` julia
62+
julia> print_results( 1000000, 0.131009101868 )
63+
```
64+
"""
65+
function print_results( iterations, elapsed )
66+
rate = iterations / elapsed
67+
68+
@printf( " ---\n" );
69+
@printf( " iterations: %d\n", iterations );
70+
@printf( " elapsed: %0.9f\n", elapsed );
71+
@printf( " rate: %0.9f\n", rate );
72+
@printf( " ...\n" );
73+
end
74+
75+
"""
76+
benchmark()
77+
78+
Run a benchmark.
79+
80+
# Notes
81+
82+
* Benchmark results are returned as a two-element array: [ iterations, elapsed ].
83+
* The number of iterations is not the true number of iterations. Instead, an 'iteration' is defined as a 'sample', which is a computed estimate for a single evaluation.
84+
* The elapsed time is in seconds.
85+
86+
# Examples
87+
88+
``` julia
89+
julia> benchmark();
90+
```
91+
"""
92+
function benchmark()
93+
t = BenchmarkTools.@benchmark ifelse( rand() > 0.5, 1.0, -1.0 ) samples=1e6
94+
95+
# Compute the total "elapsed" time and convert from nanoseconds to seconds:
96+
s = sum( t.times ) / 1.0e9;
97+
98+
# Determine the number of "iterations":
99+
iter = length( t.times );
100+
101+
# Return the results:
102+
[ iter, s ];
103+
end
104+
105+
"""
106+
main()
107+
108+
Run benchmarks.
109+
110+
# Examples
111+
112+
``` julia
113+
julia> main();
114+
```
115+
"""
116+
function main()
117+
print_version();
118+
for i in 1:3
119+
@printf( "# julia::%s\n", name );
120+
results = benchmark();
121+
print_results( results[ 1 ], results[ 2 ] );
122+
@printf( "ok %d benchmark finished\n", i );
123+
end
124+
print_summary( repeats, repeats );
125+
end
126+
127+
main();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( bool, x, y )
3+
If a condition is truthy, returns `x`; otherwise, returns `y`.
4+
5+
Parameters
6+
----------
7+
bool: boolean
8+
Condition.
9+
10+
x: any
11+
Value to return if a condition is truthy.
12+
13+
y: any
14+
Value to return if a condition is falsy.
15+
16+
Returns
17+
-------
18+
z: any
19+
Either `x` or `y`.
20+
21+
Examples
22+
--------
23+
> var z = {{alias}}( true, 'yes', 'no' )
24+
'yes'
25+
> z = {{alias}}( false, 'yes', 'no' )
26+
'no'
27+
28+
See Also
29+
--------
30+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
'use strict';
2+
3+
var randu = require( '@stdlib/math/base/random/randu' );
4+
var ifelse = require( './../lib' );
5+
6+
var z;
7+
var i;
8+
9+
for ( i = 0; i < 100; i++ ) {
10+
z = ifelse( randu() > 0.9, 'BOOP', 'beep' );
11+
console.log( z );
12+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
/**
4+
* If a condition is truthy, returns `x`; otherwise, returns `y`.
5+
*
6+
* @param {boolean} bool - condition
7+
* @param {*} x - value to return if a condition is truthy
8+
* @param {*} y - value to return if a condition is falsy
9+
* @returns {*} either `x` or `y`
10+
*
11+
* @example
12+
* var randu = require( '@stdlib/math/base/random/randu' );
13+
*
14+
* var z = ifelse( randu() > 0.5, 1.0, -1.0 );
15+
*/
16+
function ifelse( bool, x, y ) {
17+
if ( bool ) {
18+
return x;
19+
}
20+
return y;
21+
} // end FUNCTION ifelse()
22+
23+
24+
// EXPORTS //
25+
26+
module.exports = ifelse;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
/**
4+
* If a condition is truthy, return `x`; otherwise, return `y`.
5+
*
6+
* @module @stdlib/utils/if-else
7+
*
8+
* @example
9+
* var randu = require( '@stdlib/math/base/random/randu' );
10+
* var ifelse = require( '@stdlib/utils/if-else' );
11+
*
12+
* var z = ifelse( randu() > 0.5, 1.0, -1.0 );
13+
* // returns <number>
14+
*/
15+
16+
// MODULES //
17+
18+
var ifelse = require( './if_else.js' );
19+
20+
21+
// EXPORTS //
22+
23+
module.exports = ifelse;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@stdlib/utils/if-else",
3+
"version": "0.0.0",
4+
"description": "If a condition is truthy, return `x`; otherwise, return `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+
"if-else",
33+
"control",
34+
"flow",
35+
"condition",
36+
"conditional",
37+
"either-or"
38+
],
39+
"bugs": {
40+
"url": "https://github.com/stdlib-js/stdlib/issues"
41+
},
42+
"dependencies": {},
43+
"devDependencies": {},
44+
"engines": {
45+
"node": ">=0.10.0",
46+
"npm": ">2.7.0"
47+
},
48+
"license": "Apache-2.0"
49+
}

0 commit comments

Comments
 (0)