|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 The Stdlib Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +
|
| 19 | +--> |
| 20 | + |
| 21 | +# iterUntilEach |
| 22 | + |
| 23 | +> Create an iterator which, while a test condition is false, invokes a function for each iterated value before returning the iterated value. |
| 24 | +
|
| 25 | +<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> |
| 26 | + |
| 27 | +<section class="intro"> |
| 28 | + |
| 29 | +</section> |
| 30 | + |
| 31 | +<!-- /.intro --> |
| 32 | + |
| 33 | +<!-- Package usage documentation. --> |
| 34 | + |
| 35 | +<section class="usage"> |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +```javascript |
| 40 | +var iterUntilEach = require( '@stdlib/iter/until-each' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### iterUntilEach( iterator, predicate, fcn\[, thisArg] ) |
| 44 | + |
| 45 | +Returns an iterator which invokes a function for each iterated value **before** returning the iterated value until either a `predicate` function returns `true` or the iterator has iterated over all values. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 49 | + |
| 50 | +function predicate( v ) { |
| 51 | + return v > 2; |
| 52 | +} |
| 53 | + |
| 54 | +function assert( v ) { |
| 55 | + if ( v !== v ) { |
| 56 | + throw new Error( 'should not be NaN' ); |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +var it = iterUntilEach( array2iterator( [ 1, 2, 3, 4 ] ), predicate, assert ); |
| 61 | +// returns {} |
| 62 | + |
| 63 | +var r = it.next().value; |
| 64 | +// returns 1 |
| 65 | + |
| 66 | +r = it.next().value; |
| 67 | +// returns 2 |
| 68 | + |
| 69 | +r = it.next().value; |
| 70 | +// undefined |
| 71 | + |
| 72 | +// ... |
| 73 | +``` |
| 74 | + |
| 75 | +The returned iterator protocol-compliant object has the following properties: |
| 76 | + |
| 77 | +- **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a boolean value indicating whether the iterator is finished. |
| 78 | +- **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. |
| 79 | + |
| 80 | +Both the `predicate` function and the function to invoke for each iterated value are provided two arguments: |
| 81 | + |
| 82 | +- **value**: iterated value |
| 83 | +- **index**: iteration index (zero-based) |
| 84 | + |
| 85 | +```javascript |
| 86 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 87 | + |
| 88 | +function predicate( v ) { |
| 89 | + return v > 2; |
| 90 | +} |
| 91 | + |
| 92 | +function assert( v, i ) { |
| 93 | + if ( i > 1 ) { |
| 94 | + throw new Error( 'unexpected error' ); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +var it = iterUntilEach( array2iterator( [ 1, 2, 3, 4 ] ), predicate, assert ); |
| 99 | +// returns <Object> |
| 100 | + |
| 101 | +var r = it.next().value; |
| 102 | +// returns 1 |
| 103 | + |
| 104 | +r = it.next().value; |
| 105 | +// returns 2 |
| 106 | + |
| 107 | +r = it.next().value; |
| 108 | +// undefined |
| 109 | + |
| 110 | +// ... |
| 111 | +``` |
| 112 | + |
| 113 | +To set the execution context for `fcn`, provide a `thisArg`. |
| 114 | + |
| 115 | +<!-- eslint-disable no-invalid-this --> |
| 116 | + |
| 117 | +```javascript |
| 118 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 119 | + |
| 120 | +function assert( v ) { |
| 121 | + this.count += 1; |
| 122 | + if ( v !== v ) { |
| 123 | + throw new Error( 'should not be NaN' ); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +function predicate( v ) { |
| 128 | + return v > 2; |
| 129 | +} |
| 130 | + |
| 131 | +var ctx = { |
| 132 | + 'count': 0 |
| 133 | +}; |
| 134 | + |
| 135 | +var it = iterUntilEach( array2iterator( [ 1, 2, 3 ] ), predicate, assert, ctx ); |
| 136 | +// returns <Object> |
| 137 | + |
| 138 | +var r = it.next().value; |
| 139 | +// returns 1 |
| 140 | + |
| 141 | +r = it.next().value; |
| 142 | +// returns 2 |
| 143 | + |
| 144 | +var count = ctx.count; |
| 145 | +// returns 2 |
| 146 | +``` |
| 147 | + |
| 148 | +</section> |
| 149 | + |
| 150 | +<!-- /.usage --> |
| 151 | + |
| 152 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 153 | + |
| 154 | +<section class="notes"> |
| 155 | + |
| 156 | +## Notes |
| 157 | + |
| 158 | +- If an environment supports `Symbol.iterator` **and** a provided iterator is iterable, the returned iterator is iterable. |
| 159 | + |
| 160 | +</section> |
| 161 | + |
| 162 | +<!-- /.notes --> |
| 163 | + |
| 164 | +<!-- Package usage examples. --> |
| 165 | + |
| 166 | +<section class="examples"> |
| 167 | + |
| 168 | +## Examples |
| 169 | + |
| 170 | +<!-- eslint no-undef: "error" --> |
| 171 | + |
| 172 | +```javascript |
| 173 | +var randu = require( '@stdlib/random/iter/randu' ); |
| 174 | +var isnan = require( '@stdlib/math/base/assert/is-nan' ); |
| 175 | +var iterUntilEach = require( '@stdlib/iter/until-each' ); |
| 176 | + |
| 177 | +function assert( v ) { |
| 178 | + if ( isnan( v ) ) { |
| 179 | + throw new Error( 'should not be NaN' ); |
| 180 | + } |
| 181 | +} |
| 182 | + |
| 183 | +function predicate( v ) { |
| 184 | + return v <= 0.75; |
| 185 | +} |
| 186 | + |
| 187 | +// Create a seeded iterator for generating pseudorandom numbers: |
| 188 | +var rand = randu({ |
| 189 | + 'seed': 1234, |
| 190 | + 'iter': 10 |
| 191 | +}); |
| 192 | + |
| 193 | +// Create an iterator which validates generated numbers: |
| 194 | +var it = iterUntilEach( rand, predicate, assert ); |
| 195 | + |
| 196 | +// Perform manual iteration... |
| 197 | +var r; |
| 198 | +while ( true ) { |
| 199 | + r = it.next(); |
| 200 | + if ( r.done ) { |
| 201 | + break; |
| 202 | + } |
| 203 | + console.log( r.value ); |
| 204 | +} |
| 205 | +``` |
| 206 | + |
| 207 | +</section> |
| 208 | + |
| 209 | +<!-- /.examples --> |
| 210 | + |
| 211 | +<!-- 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. --> |
| 212 | + |
| 213 | +<section class="references"> |
| 214 | + |
| 215 | +</section> |
| 216 | + |
| 217 | +<!-- /.references --> |
| 218 | + |
| 219 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 220 | + |
| 221 | +<section class="related"> |
| 222 | + |
| 223 | +</section> |
| 224 | + |
| 225 | +<!-- /.related --> |
| 226 | + |
| 227 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 228 | + |
| 229 | +<section class="links"> |
| 230 | + |
| 231 | +</section> |
| 232 | + |
| 233 | +<!-- /.links --> |
0 commit comments