time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (9551B)


      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 # everyByRightAsync
     22 
     23 > Test whether all elements in a collection pass a test implemented by a predicate function, iterating from right to left.
     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 everyByRightAsync = require( '@stdlib/utils/async/every-by-right' );
     41 ```
     42 
     43 #### everyByRightAsync( collection, \[options,] predicate, done )
     44 
     45 Tests whether all elements in a `collection` pass a test implemented by a `predicate` function, iterating from right to left.
     46 
     47 ```javascript
     48 function predicate( value, next ) {
     49     setTimeout( onTimeout, value );
     50     function onTimeout() {
     51         console.log( value );
     52         next( null, true );
     53     }
     54 }
     55 
     56 function done( error, bool ) {
     57     if ( error ) {
     58         throw error;
     59     }
     60     console.log( bool );
     61 }
     62 
     63 var arr = [ 1000, 2500, 3000 ];
     64 
     65 everyByRightAsync( arr, predicate, done );
     66 /* =>
     67     1000
     68     2500
     69     3000
     70     true
     71 */
     72 ```
     73 
     74 If a `predicate` function calls the `next` callback with a non-truthy test argument, the function stops processing any additional `collection` elements and returns `false` for the test result.
     75 
     76 ```javascript
     77 function predicate( value, index, next ) {
     78     setTimeout( onTimeout, value );
     79     function onTimeout() {
     80         if ( index === 1 ) {
     81             return next( null, false );
     82         }
     83         next( null, true );
     84     }
     85 }
     86 
     87 function done( error, bool ) {
     88     if ( error ) {
     89         throw error;
     90     }
     91     console.log( bool );
     92 }
     93 
     94 var arr = [ 1000, 2500, 3000 ];
     95 
     96 everyByRightAsync( arr, predicate, done );
     97 // => false
     98 ```
     99 
    100 The function accepts the following `options`:
    101 
    102 -   `limit`: the maximum number of pending invocations at any one time. Default: `infinity`.
    103 -   `series`: `boolean` indicating whether to sequentially invoke the `predicate` function for each `collection` element. If `true`, the function sets `options.limit=1`. Default: `false`.
    104 -   `thisArg`: the execution context for `fcn`.
    105 
    106 By default, all elements are processed concurrently, which means that the function does **not** guarantee completion order. To process each `collection` element sequentially, set the `series` option to `true`.
    107 
    108 ```javascript
    109 function predicate( value, next ) {
    110     setTimeout( onTimeout, value );
    111     function onTimeout() {
    112         console.log( value );
    113         next( null, true );
    114     }
    115 }
    116 
    117 function done( error, bool ) {
    118     if ( error ) {
    119         throw error;
    120     }
    121     console.log( bool );
    122 }
    123 
    124 var arr = [ 1000, 2500, 3000 ];
    125 
    126 var opts = {
    127     'series': true
    128 };
    129 
    130 everyByRightAsync( arr, opts, predicate, done );
    131 /* =>
    132     3000
    133     2500
    134     1000
    135     true
    136 */
    137 ```
    138 
    139 To limit the maximum number of pending function invocations, set the `limit` option.
    140 
    141 ```javascript
    142 function predicate( value, next ) {
    143     setTimeout( onTimeout, value );
    144     function onTimeout() {
    145         console.log( value );
    146         next( null, true );
    147     }
    148 }
    149 
    150 function done( error, bool ) {
    151     if ( error ) {
    152         throw error;
    153     }
    154     console.log( bool );
    155 }
    156 
    157 var arr = [ 1000, 2500, 3000 ];
    158 
    159 var opts = {
    160     'limit': 2
    161 };
    162 
    163 everyByRightAsync( arr, opts, predicate, done );
    164 /* =>
    165     2500
    166     3000
    167     1000
    168     true
    169 */
    170 ```
    171 
    172 To set the execution context of the `predicate` function, set the `thisArg` option.
    173 
    174 ```javascript
    175 function predicate( value, next ) {
    176     this.count += 1;
    177     setTimeout( onTimeout, value );
    178     function onTimeout() {
    179         next( null, true );
    180     }
    181 }
    182 
    183 var arr = [ 1000, 2500, 3000 ];
    184 
    185 var context = {
    186     'count': 0
    187 };
    188 
    189 var opts = {
    190     'thisArg': context
    191 };
    192 
    193 everyByRightAsync( arr, opts, predicate, done );
    194 
    195 function done( error, bool ) {
    196     if ( error ) {
    197         throw error;
    198     }
    199     console.log( bool );
    200     // => true
    201 
    202     console.log( context.count );
    203     // => 3
    204 }
    205 ```
    206 
    207 When invoked, the `predicate` function is provided a maximum of four arguments:
    208 
    209 -   `value`: collection value.
    210 -   `index`: collection index.
    211 -   `collection`: the input `collection`.
    212 -   `next`: a callback which should be called once the `predicate` function has finished processing a collection `value`.
    213 
    214 The actual number of provided arguments depends on function `length`. If the `predicate` function accepts two arguments, the `predicate` function is provided `value` and `next`. If the `predicate` function accepts three arguments, the `predicate` function is provided `value`, `index`, and `next`. For every other `predicate` function signature, the `predicate` function is provided all four arguments.
    215 
    216 ```javascript
    217 function predicate( value, i, collection, next ) {
    218     console.log( 'collection: %s. %d: %d', collection.join( ',' ), i, value );
    219     setTimeout( onTimeout, value );
    220     function onTimeout() {
    221         console.log( value );
    222         next( null, true );
    223     }
    224 }
    225 
    226 function done( error, bool ) {
    227     if ( error ) {
    228         throw error;
    229     }
    230     console.log( bool );
    231 }
    232 
    233 var arr = [ 1000, 2500, 3000 ];
    234 
    235 everyByRightAsync( arr, predicate, done );
    236 /* =>
    237     collection: 3000,2500,1000. 2: 3000
    238     collection: 3000,2500,1000. 1: 2500
    239     collection: 3000,2500,1000. 0: 1000
    240     1000
    241     2500
    242     3000
    243     true
    244 */
    245 ```
    246 
    247 #### everyByRightAsync.factory( \[options,] predicate )
    248 
    249 Returns a `function` which invokes a `predicate` function once for each element in a `collection`, iterating from right to left.
    250 
    251 ```javascript
    252 function predicate( value, next ) {
    253     setTimeout( onTimeout, value );
    254     function onTimeout() {
    255         console.log( value );
    256         next( null, true );
    257     }
    258 }
    259 
    260 function done( error, bool ) {
    261     if ( error ) {
    262         throw error;
    263     }
    264     console.log( bool );
    265 }
    266 
    267 var f = everyByRightAsync.factory( predicate );
    268 
    269 var arr1 = [ 1000, 2500, 3000 ];
    270 
    271 f( arr1, done );
    272 /* =>
    273     1000
    274     2500
    275     3000
    276     true
    277 */
    278 
    279 var arr2 = [ 100, 250, 300 ];
    280 
    281 f( arr2, done );
    282 /* =>
    283     100
    284     250
    285     300
    286     true
    287 */
    288 ```
    289 
    290 The function accepts the same `options` as `everyByRightAsync()`.
    291 
    292 </section>
    293 
    294 <!-- /.usage -->
    295 
    296 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    297 
    298 <section class="notes">
    299 
    300 ## Notes
    301 
    302 -   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`).
    303 -   If a provided function calls the `next` callback with a truthy `error` argument, the function suspends execution and immediately calls the `done` callback for subsequent `error` handling.
    304 -   The function does **not** support dynamic `collection` resizing.
    305 -   The function does **not** skip `undefined` elements.
    306 -   If provided an empty `collection`, the function calls the `done` callback with `true` as the test result.
    307 -   **Neither** `everyByRightAsync` nor the function returned by the `factory` method **guarantee** asynchronous execution. To guarantee asynchrony, wrap the `done` callback in a function which either executes at the end of the current stack (e.g., `nextTick`) or during a subsequent turn of the event loop (e.g., `setImmediate`, `setTimeout`).
    308 
    309 </section>
    310 
    311 <!-- /.notes -->
    312 
    313 <!-- Package usage examples. -->
    314 
    315 <section class="examples">
    316 
    317 ## Examples
    318 
    319 <!-- eslint no-undef: "error" -->
    320 
    321 ```javascript
    322 var resolve = require( 'path' ).resolve;
    323 var readFile = require( '@stdlib/fs/read-file' );
    324 var everyByRightAsync = require( '@stdlib/utils/async/every-by-right' );
    325 
    326 var files = [
    327     resolve( __dirname, 'package.json' ),
    328     resolve( __dirname, 'README.md' )
    329 ];
    330 
    331 function done( error, bool ) {
    332     if ( error ) {
    333         throw error;
    334     }
    335     if ( bool ) {
    336         console.log( 'Successfully read all files.' );
    337     } else {
    338         console.log( 'Was unable to read all files.' );
    339     }
    340 }
    341 
    342 function predicate( file, next ) {
    343     var opts = {
    344         'encoding': 'utf8'
    345     };
    346     readFile( file, opts, onFile );
    347 
    348     function onFile( error ) {
    349         if ( error ) {
    350             return next( null, false );
    351         }
    352         next( null, true );
    353     }
    354 }
    355 
    356 everyByRightAsync( files, predicate, done );
    357 ```
    358 
    359 </section>
    360 
    361 <!-- /.examples -->
    362 
    363 <!-- 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. -->
    364 
    365 <section class="references">
    366 
    367 </section>
    368 
    369 <!-- /.references -->
    370 
    371 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    372 
    373 <section class="links">
    374 
    375 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    376 
    377 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    378 
    379 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
    380 
    381 </section>
    382 
    383 <!-- /.links -->