README.md (5634B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 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 # untilEach 22 23 > Until a test condition is true, invoke a function for each element in a collection. 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 untilEach = require( '@stdlib/utils/until-each' ); 41 ``` 42 43 #### untilEach( collection, predicate, fcn\[, thisArg ] ) 44 45 Invokes a `function` for each element in a `collection` until either a `predicate` function returns `true` or the function has iterated over all `collection` elements. 46 47 ```javascript 48 function predicate( value ) { 49 return ( value !== value ); 50 } 51 52 function log( value, index ) { 53 console.log( '%s: %d', index, value ); 54 } 55 56 var arr = [ 1, 2, 3, NaN, 4 ]; 57 58 untilEach( arr, predicate, log ); 59 /* => 60 0: 1 61 1: 2 62 2: 3 63 */ 64 ``` 65 66 Both the `predicate` function and the `function` to apply are provided three arguments: 67 68 - `value`: collection element 69 - `index`: collection index 70 - `collection`: input collection 71 72 Basic support for dynamic collections is provided. Note, however, that index incrementation is monotonically increasing. 73 74 ```javascript 75 function predicate( value ) { 76 return ( value !== value ); 77 } 78 79 function log1( value, index, collection ) { 80 console.log( '%s: %d', index, value ); 81 if ( index === collection.length-1 && collection.length < 10 ) { 82 collection.push( index+2 ); 83 } 84 } 85 86 var arr = [ 1, 2, 3, 4 ]; 87 88 untilEach( arr, predicate, log1 ); 89 /* => 90 0: 1 91 1: 2 92 2: 3 93 3: 4 94 4: 5 95 5: 6 96 6: 7 97 7: 8 98 8: 9 99 9: 10 100 */ 101 102 function log2( value, index, collection ) { 103 console.log( '%s: %d', index, value ); 104 collection.shift(); 105 } 106 107 arr = [ 1, 2, 3, 4 ]; 108 109 untilEach( arr, predicate, log2 ); 110 /* => 111 0: 1 112 1: 3 113 */ 114 ``` 115 116 To set the function execution context for the applied function, provide a `thisArg`. 117 118 ```javascript 119 function predicate( value ) { 120 return ( value !== value ); 121 } 122 123 function sum( value ) { 124 this.sum += value; 125 this.count += 1; 126 } 127 128 var arr = [ 1, 2, 3, NaN, 4 ]; 129 130 var context = { 131 'sum': 0, 132 'count': 0 133 }; 134 135 untilEach( arr, predicate, sum, context ); 136 137 var mean = context.sum / context.count; 138 // returns 2.0 139 ``` 140 141 </section> 142 143 <!-- /.usage --> 144 145 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 146 147 <section class="notes"> 148 149 ## Notes 150 151 - A `collection` may be either an [`Array`][mdn-array], [`Typed Array`][mdn-typed-array], or an array-like [`Object`][mdn-object] (excluding `strings` and `functions`). 152 153 - The function returns the input `collection`. 154 155 - The function does **not** skip `undefined` elements. 156 157 <!-- eslint-disable no-sparse-arrays --> 158 159 ```javascript 160 function predicate( value ) { 161 return ( value !== value ); 162 } 163 164 function log( value, index ) { 165 console.log( '%s: %s', index, value ); 166 } 167 168 var arr = [ 1, , , 4 ]; 169 170 untilEach( arr, predicate, log ); 171 /* => 172 0: 1 173 1: undefined 174 2: undefined 175 3: 4 176 */ 177 ``` 178 179 - The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution). 180 181 </section> 182 183 <!-- /.notes --> 184 185 <!-- Package usage examples. --> 186 187 <section class="examples"> 188 189 ## Examples 190 191 <!-- eslint no-undef: "error" --> 192 193 ```javascript 194 var isEven = require( '@stdlib/assert/is-even' ).isPrimitive; 195 var randu = require( '@stdlib/random/base/randu' ); 196 var floor = require( '@stdlib/math/base/special/floor' ); 197 var untilEach = require( '@stdlib/utils/until-each' ); 198 199 function predicate( value ) { 200 return ( value !== value ); 201 } 202 203 function log( value, index, collection ) { 204 console.log( '%s: %d', index, value ); 205 if ( isEven( index ) ) { 206 collection.shift(); 207 } else { 208 collection.push( index+1 ); 209 } 210 } 211 212 var arr; 213 var j; 214 var i; 215 216 arr = new Array( 100 ); 217 j = floor( randu()*arr.length ); 218 for ( i = 0; i < arr.length; i++ ) { 219 if ( i === j ) { 220 arr[ i ] = NaN; 221 } else { 222 arr[ i ] = i; 223 } 224 } 225 226 untilEach( arr, predicate, log ); 227 ``` 228 229 </section> 230 231 <!-- /.examples --> 232 233 <!-- 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. --> 234 235 <section class="references"> 236 237 </section> 238 239 <!-- /.references --> 240 241 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 242 243 <section class="links"> 244 245 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array 246 247 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray 248 249 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object 250 251 </section> 252 253 <!-- /.links -->