time-to-botec

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

README.md (5803B)


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