|
| 1 | +# doUntil |
| 2 | + |
| 3 | +> Invoke a function until a test condition is true. |
| 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 doUntil = require( '@stdlib/utils/do-until' ); |
| 22 | +``` |
| 23 | + |
| 24 | +#### doUntil( predicate, fcn\[, thisArg \] ) |
| 25 | + |
| 26 | +Invokes a `function` until a `predicate` function returns `true`. 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 | +doUntil( 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 | +doUntil( 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 doUntil = require( '@stdlib/utils/do-until' ); |
| 94 | + |
| 95 | +function predicate() { |
| 96 | + return ( randu() <= 0.05 ); |
| 97 | +} |
| 98 | + |
| 99 | +function log( i ) { |
| 100 | + console.log( i ); |
| 101 | +} |
| 102 | + |
| 103 | +doUntil( 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 --> |
0 commit comments