time-to-botec

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

README.md (5465B)


      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 # inmapRight
     22 
     23 > Invoke a function for each element in a collection and update the collection in-place, 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 inmapRight = require( '@stdlib/utils/inmap-right' );
     41 ```
     42 
     43 #### inmapRight( collection, fcn\[, thisArg ] )
     44 
     45 Invokes a `function` for each element in a `collection` and updates the `collection` in-place, iterating from right to left.
     46 
     47 ```javascript
     48 function scale( value, index ) {
     49     return value * index;
     50 }
     51 
     52 var arr = [ 1, 2, 3, 4 ];
     53 
     54 var out = inmapRight( arr, scale );
     55 // returns [ 0, 2, 6, 12 ]
     56 
     57 var bool = ( out === arr );
     58 // returns true
     59 ```
     60 
     61 The invoked `function` is provided three arguments:
     62 
     63 -   `value`: collection element
     64 -   `index`: collection index
     65 -   `collection`: input collection
     66 
     67 Basic support for dynamic collections is provided. Note, however, that index incrementation is **not** guaranteed to be monotonically **decreasing**.
     68 
     69 ```javascript
     70 var arr = [ 1, 2, 3, 4 ];
     71 var i = 0;
     72 
     73 function scale1( value, index, collection ) {
     74     i += 1;
     75     if ( index === 0 && collection.length < 10 ) {
     76         collection.unshift( i+1 );
     77     }
     78     return value * index;
     79 }
     80 
     81 var out = inmapRight( arr, scale1 );
     82 // returns [ 0, 0, 0, 0, 0, 0, 0, 2, 6, 12 ]
     83 
     84 function scale2( value, index, collection ) {
     85     collection.shift();
     86     return value * index;
     87 }
     88 
     89 arr = [ 1, 2, 3, 4 ];
     90 
     91 out = inmapRight( arr, scale2 );
     92 // returns [ 3, 12 ]
     93 ```
     94 
     95 To set the function execution context, provide a `thisArg`.
     96 
     97 ```javascript
     98 function sum( value ) {
     99     this.sum += value;
    100     this.count += 1;
    101     return value;
    102 }
    103 
    104 var arr = [ 1, 2, 3, 4 ];
    105 
    106 var context = {
    107     'sum': 0,
    108     'count': 0
    109 };
    110 
    111 var out = inmapRight( arr, sum, context );
    112 // returns [ 1, 2, 3, 4 ]
    113 
    114 var mean = context.sum / context.count;
    115 // returns 2.5
    116 ```
    117 
    118 </section>
    119 
    120 <!-- /.usage -->
    121 
    122 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    123 
    124 <section class="notes">
    125 
    126 ## Notes
    127 
    128 -   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`).
    129 
    130 -   The function differs from [`Array.prototype.map`][mdn-array-map] in the following ways:
    131 
    132     -   The function returns the input `collection`.
    133 
    134     -   The function modifies `collection` elements in-place.
    135 
    136     -   The function does **not** skip `undefined` elements.
    137 
    138         <!-- eslint-disable no-sparse-arrays, stdlib/doctest-marker -->
    139 
    140         ```javascript
    141         function log( value, index ) {
    142             console.log( '%s: %s', index, value );
    143             return value;
    144         }
    145 
    146         var arr = [ 1, , , 4 ];
    147 
    148         var out = inmapRight( arr, log );
    149         /* =>
    150             3: 4
    151             2: undefined
    152             1: undefined
    153             0: 1
    154         */
    155         ```
    156 
    157     -   The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution).
    158 
    159 </section>
    160 
    161 <!-- /.notes -->
    162 
    163 <!-- Package usage examples. -->
    164 
    165 <section class="examples">
    166 
    167 ## Examples
    168 
    169 <!-- eslint no-undef: "error" -->
    170 
    171 ```javascript
    172 var isEven = require( '@stdlib/assert/is-even' ).isPrimitive;
    173 var inmapRight = require( '@stdlib/utils/inmap-right' );
    174 
    175 var bool;
    176 var arr;
    177 var out;
    178 var i;
    179 
    180 function scale( value, index, collection ) {
    181     i += 1;
    182     if ( isEven( i ) ) {
    183         collection.pop();
    184     } else {
    185         collection.unshift( i+1 );
    186     }
    187     return value * index;
    188 }
    189 
    190 arr = new Array( 100 );
    191 for ( i = 0; i < arr.length; i++ ) {
    192     arr[ i ] = i;
    193 }
    194 
    195 i = 0;
    196 out = inmapRight( arr, scale );
    197 
    198 bool = ( out === arr );
    199 console.log( bool );
    200 
    201 console.log( out );
    202 ```
    203 
    204 </section>
    205 
    206 <!-- /.examples -->
    207 
    208 <!-- 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. -->
    209 
    210 <section class="references">
    211 
    212 </section>
    213 
    214 <!-- /.references -->
    215 
    216 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    217 
    218 <section class="links">
    219 
    220 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    221 
    222 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    223 
    224 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
    225 
    226 [mdn-array-map]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
    227 
    228 </section>
    229 
    230 <!-- /.links -->