Skip to content

Commit a3b1915

Browse files
committed
Add a functional do-while utility
1 parent 063ac1e commit a3b1915

File tree

8 files changed

+479
-0
lines changed

8 files changed

+479
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# doWhile
2+
3+
> While a test condition is true, invoke a function.
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 doWhile = require( '@stdlib/utils/do-while' );
22+
```
23+
24+
#### doWhile( predicate, fcn\[, thisArg \] )
25+
26+
Invokes a `function` until a `predicate` function returns `false`. Note that the `predicate` function is evaluated __after__ executing `fcn`; thus, `fcn` __always__ executes at least once.
27+
28+
``` javascript
29+
function predicate( i ) {
30+
return ( i < 5 );
31+
}
32+
33+
function beep( i ) {
34+
console.log( 'boop: %d', i );
35+
}
36+
37+
doWhile( predicate, beep );
38+
/* =>
39+
boop: 0
40+
boop: 1
41+
boop: 2
42+
boop: 3
43+
boop: 4
44+
*/
45+
```
46+
47+
Both the `predicate` function and the `function` to invoke are provided a single argument:
48+
49+
* `i`: iteration number (starting from zero)
50+
51+
To set the function execution context for the invoked function, provide a `thisArg`.
52+
53+
``` javascript
54+
function predicate( i ) {
55+
return ( i < 5 );
56+
}
57+
58+
function count() {
59+
this.count += 1;
60+
}
61+
62+
var context = {
63+
'count': 0
64+
};
65+
66+
doWhile( predicate, count, context );
67+
68+
console.log( context.count );
69+
// => 5
70+
```
71+
72+
73+
</section>
74+
75+
<!-- /.usage -->
76+
77+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
78+
79+
<section class="notes">
80+
81+
</section>
82+
83+
<!-- /.notes -->
84+
85+
<!-- Package usage examples. -->
86+
87+
<section class="examples">
88+
89+
## Examples
90+
91+
``` javascript
92+
var randu = require( '@stdlib/math/base/random/randu' );
93+
var doWhile = require( '@stdlib/utils/do-while' );
94+
95+
function predicate() {
96+
return ( randu() > 0.05 );
97+
}
98+
99+
function log( i ) {
100+
console.log( i );
101+
}
102+
103+
doWhile( predicate, log );
104+
```
105+
106+
</section>
107+
108+
<!-- /.examples -->
109+
110+
<!-- 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. -->
111+
112+
<section class="references">
113+
114+
</section>
115+
116+
<!-- /.references -->
117+
118+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
119+
120+
<section class="links">
121+
122+
</section>
123+
124+
<!-- /.links -->
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var bench = require( '@stdlib/bench' );
6+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
7+
var pkg = require( './../package.json' ).name;
8+
var doWhile = require( './../lib' );
9+
10+
11+
// MAIN //
12+
13+
bench( pkg, function benchmark( b ) {
14+
var sum;
15+
var i;
16+
17+
function predicate( i ) {
18+
return ( i < 10 );
19+
}
20+
21+
function fcn( i ) {
22+
sum += i;
23+
}
24+
25+
b.tic();
26+
for ( i = 0; i < b.iterations; i++ ) {
27+
sum = 0;
28+
doWhile( predicate, fcn );
29+
if ( isnan( sum ) ) {
30+
b.fail( 'should not be NaN' );
31+
}
32+
}
33+
b.toc();
34+
if ( isnan( sum ) ) {
35+
b.fail( 'should not be NaN' );
36+
}
37+
b.pass( 'benchmark finished' );
38+
b.end();
39+
});
40+
41+
bench( pkg+'::loop', function benchmark( b ) {
42+
var sum;
43+
var i;
44+
var j;
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
sum = 0;
49+
j = 0;
50+
do {
51+
sum += j;
52+
j += 1;
53+
} while ( j < 10 );
54+
if ( isnan( sum ) ) {
55+
b.fail( 'should not be NaN' );
56+
}
57+
}
58+
b.toc();
59+
if ( isnan( sum ) ) {
60+
b.fail( 'should not be NaN' );
61+
}
62+
b.pass( 'benchmark finished' );
63+
b.end();
64+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
{{alias}}( predicate, fcn[, thisArg] )
3+
While a test condition is true, invokes a function.
4+
5+
The condition is evaluated *after* executing the provided function; thus,
6+
`fcn` *always* executes at least once.
7+
8+
When invoked, both the predicate function and the function to invoke are
9+
provided a single argument:
10+
11+
- `i`: iteration number (starting from zero)
12+
13+
Parameters
14+
----------
15+
predicate: Function
16+
The predicate function which indicates whether to continue invoking a
17+
function.
18+
19+
fcn: Function
20+
The function to invoke.
21+
22+
thisArg: any (optional)
23+
Execution context for the invoked function.
24+
25+
Examples
26+
--------
27+
> function predicate( i ) { return ( i < 5 ); };
28+
> function beep( i ) { console.log( 'boop: %d', i ); };
29+
> {{alias}}( predicate, beep )
30+
boop: 0
31+
boop: 1
32+
boop: 2
33+
boop: 3
34+
boop: 4
35+
36+
See Also
37+
--------
38+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
'use strict';
2+
3+
var randu = require( '@stdlib/math/base/random/randu' );
4+
var doWhile = require( './../lib' );
5+
6+
function predicate() {
7+
return ( randu() > 0.05 );
8+
}
9+
10+
function log( i ) {
11+
console.log( i );
12+
}
13+
14+
doWhile( predicate, log );
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
'use strict';
2+
3+
// MODULES //
4+
5+
var isFunction = require( '@stdlib/assert/is-function' );
6+
7+
8+
// MAIN //
9+
10+
/**
11+
* While a test condition is true, invokes a function.
12+
*
13+
* @param {Function} predicate - function which indicates whether to continue invoking a function
14+
* @param {Function} fcn - function to invoke
15+
* @param {*} [thisArg] - execution context for the invoked function
16+
* @throws {TypeError} first argument must be a function
17+
* @throws {TypeError} second argument must be a function
18+
*
19+
* @example
20+
* function predicate( i ) {
21+
* return ( i < 5 );
22+
* }
23+
*
24+
* function beep( i ) {
25+
* console.log( 'beep: %d', i );
26+
* }
27+
*
28+
* doWhile( predicate, beep );
29+
*/
30+
function doWhile( predicate, fcn, thisArg ) {
31+
var i;
32+
if ( !isFunction( predicate ) ) {
33+
throw new TypeError( 'invalid input argument. First argument must be a function. Value: `'+predicate+'`.' );
34+
}
35+
if ( !isFunction( fcn ) ) {
36+
throw new TypeError( 'invalid input argument. Second argument must be a function. Value: `'+fcn+'`.' );
37+
}
38+
i = 0;
39+
do {
40+
fcn.call( thisArg, i );
41+
i += 1;
42+
} while ( predicate( i ) );
43+
} // end FUNCTION doWhile()
44+
45+
46+
// EXPORTS //
47+
48+
module.exports = doWhile;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'use strict';
2+
3+
/**
4+
* While a test condition is true, invoke a function.
5+
*
6+
* @module @stdlib/utils/do-while
7+
*
8+
* @example
9+
* var doWhile = require( '@stdlib/utils/do-while' );
10+
*
11+
* function predicate( i ) {
12+
* return ( i < 5 );
13+
* }
14+
*
15+
* function beep( i ) {
16+
* console.log( 'boop: %d', i );
17+
* }
18+
*
19+
* doWhile( predicate, beep );
20+
*/
21+
22+
// MODULES //
23+
24+
var doWhile = require( './do_while.js' );
25+
26+
27+
// EXPORTS //
28+
29+
module.exports = doWhile;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
{
2+
"name": "@stdlib/utils/do-while",
3+
"version": "0.0.0",
4+
"description": "While a test condition is true, invoke a function.",
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+
"for",
31+
"while",
32+
"do-while",
33+
"do while",
34+
"do",
35+
"loop",
36+
"iterate",
37+
"iteration"
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)