time-to-botec

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

README.md (3959B)


      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 # keyByRight
     22 
     23 > Convert a collection to an object whose keys are determined by a provided function and whose values are the collection values, 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 keyByRight = require( '@stdlib/utils/key-by-right' );
     41 ```
     42 
     43 #### keyByRight( collection, fcn\[, thisArg ] )
     44 
     45 Converts a `collection` to an object whose keys are determined by a provided `function` and whose values are the `collection` values, iterating from right to left.
     46 
     47 <!-- eslint-disable object-curly-newline -->
     48 
     49 ```javascript
     50 function toKey( value ) {
     51     return value.a;
     52 }
     53 
     54 var arr = [
     55     { 'a': 1 },
     56     { 'a': 2 }
     57 ];
     58 
     59 var out = keyByRight( arr, toKey );
     60 // returns { '2': { 'a': 2 }, '1': { 'a': 1 } }
     61 ```
     62 
     63 The invoked `function` is provided two arguments:
     64 
     65 -   `value`: collection element
     66 -   `index`: collection index
     67 
     68 To set the function execution context, provide a `thisArg`.
     69 
     70 ```javascript
     71 function toKey( value, index ) {
     72     this.sum += value;
     73     this.count += 1;
     74     return index;
     75 }
     76 
     77 var arr = [ 1, 2, 3, 4 ];
     78 
     79 var context = {
     80     'sum': 0,
     81     'count': 0
     82 };
     83 
     84 var out = keyByRight( arr, toKey, context );
     85 // returns { '3': 4, '2': 3, '1': 2, '0': 1 }
     86 
     87 var mean = context.sum / context.count;
     88 // returns 2.5
     89 ```
     90 
     91 </section>
     92 
     93 <!-- /.usage -->
     94 
     95 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
     96 
     97 <section class="notes">
     98 
     99 ## Notes
    100 
    101 -   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`).
    102 -   If more than one element in a `collection` resolves to the same key, the key value is the `collection` element which last resolved to the key.
    103 -   Object values are shallow copies.
    104 
    105 </section>
    106 
    107 <!-- /.notes -->
    108 
    109 <!-- Package usage examples. -->
    110 
    111 <section class="examples">
    112 
    113 ## Examples
    114 
    115 <!-- eslint no-undef: "error" -->
    116 
    117 ```javascript
    118 var keyByRight = require( '@stdlib/utils/key-by-right' );
    119 
    120 var arr;
    121 var obj;
    122 var i;
    123 
    124 function toKey( value ) {
    125     return value.name;
    126 }
    127 
    128 arr = new Array( 100 );
    129 for ( i = 0; i < arr.length; i++ ) {
    130     arr[ i ] = {
    131         'name': 'v'+i,
    132         'value': i
    133     };
    134 }
    135 
    136 obj = keyByRight( arr, toKey );
    137 console.log( obj );
    138 ```
    139 
    140 </section>
    141 
    142 <!-- /.examples -->
    143 
    144 <!-- 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. -->
    145 
    146 <section class="references">
    147 
    148 </section>
    149 
    150 <!-- /.references -->
    151 
    152 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    153 
    154 <section class="links">
    155 
    156 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    157 
    158 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    159 
    160 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
    161 
    162 </section>
    163 
    164 <!-- /.links -->