time-to-botec

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

README.md (5751B)


      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 # groupBy
     22 
     23 > Group values according to an indicator function.
     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 groupBy = require( '@stdlib/utils/group-by' );
     41 ```
     42 
     43 #### groupBy( collection, \[options,] indicator )
     44 
     45 Groups values according to an `indicator` function, which specifies which group an element in the input `collection` belongs to.
     46 
     47 ```javascript
     48 function indicator( v ) {
     49     return v[ 0 ];
     50 }
     51 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     52 
     53 var out = groupBy( arr, indicator );
     54 // returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
     55 ```
     56 
     57 An `indicator` function is provided two arguments:
     58 
     59 -   `value`: collection element
     60 -   `index`: collection index
     61 
     62 ```javascript
     63 function indicator( v, i ) {
     64     console.log( '%d: %s', i, v );
     65     return v[ 0 ];
     66 }
     67 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     68 
     69 var out = groupBy( arr, indicator );
     70 // returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
     71 ```
     72 
     73 The function accepts the following `options`:
     74 
     75 -   `returns`: specifies the output format. If the option equals `'values'`, the function outputs element values. If the option equals `'indices'`, the function outputs element indices. If the option equals `'*'`, the function outputs both element indices and values. Default: `'values'`.
     76 -   `thisArg`: execution context.
     77 
     78 By default, the function returns element values. To return element indices, set the `returns` option to `'indices'`.
     79 
     80 ```javascript
     81 function indicator( v ) {
     82     return v[ 0 ];
     83 }
     84 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     85 
     86 var opts = {
     87     'returns': 'indices'
     88 };
     89 var out = groupBy( arr, opts, indicator );
     90 // returns { 'b': [ 0, 1, 3 ], 'f': [ 2 ] }
     91 ```
     92 
     93 To return index-element pairs, set the `returns` option to `'*'`.
     94 
     95 ```javascript
     96 function indicator( v ) {
     97     return v[ 0 ];
     98 }
     99 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
    100 
    101 var opts = {
    102     'returns': '*'
    103 };
    104 var out = groupBy( arr, opts, indicator );
    105 // returns { 'b': [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], 'f': [ [ 2, 'foo' ] ] }
    106 ```
    107 
    108 To set the `indicator` execution context, provide a `thisArg`.
    109 
    110 ```javascript
    111 function indicator( v ) {
    112     this.count += 1;
    113     return v[ 0 ];
    114 }
    115 var context = {
    116     'count': 0
    117 };
    118 var opts = {
    119     'thisArg': context
    120 };
    121 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
    122 
    123 var out = groupBy( arr, opts, indicator );
    124 // returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
    125 
    126 console.log( context.count );
    127 // => 4
    128 ```
    129 
    130 </section>
    131 
    132 <!-- /.usage -->
    133 
    134 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    135 
    136 <section class="notes">
    137 
    138 ## Notes
    139 
    140 -   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`).
    141 
    142 -   The value returned by an `indicator` function should be a value which can be serialized as an `object` key. As a counterexample,
    143 
    144     ```javascript
    145     function indicator( v ) {
    146         return {};
    147     }
    148     var arr = [ 'beep', 'boop', 'foo', 'bar' ];
    149 
    150     var out = groupBy( arr, indicator );
    151     // returns { '[object Object]': [ 'beep', 'boop', 'foo', 'bar' ] }
    152     ```
    153 
    154     while each group identifier is unique, all collection elements resolve to the same group because each group identifier serializes to the same `string`. 
    155 
    156 </section>
    157 
    158 <!-- /.notes -->
    159 
    160 <!-- Package usage examples. -->
    161 
    162 <section class="examples">
    163 
    164 ## Examples
    165 
    166 <!-- eslint no-undef: "error" -->
    167 
    168 ```javascript
    169 var randu = require( '@stdlib/random/base/randu' );
    170 var floor = require( '@stdlib/math/base/special/floor' );
    171 var groupBy = require( '@stdlib/utils/group-by' );
    172 
    173 var vals;
    174 var arr;
    175 var out;
    176 var i;
    177 var j;
    178 
    179 vals = [ 'beep', 'boop', 'foo', 'bar', 'woot', 'woot' ];
    180 
    181 // Generate a random collection...
    182 arr = new Array( 100 );
    183 for ( i = 0; i < arr.length; i++ ) {
    184     j = floor( randu()*vals.length );
    185     arr[ i ] = vals[ j ];
    186 }
    187 
    188 function indicator( v ) {
    189     // Use the first letter of each element to define groups:
    190     return v[ 0 ];
    191 }
    192 
    193 // Compute the groups:
    194 out = groupBy( arr, indicator );
    195 console.log( out );
    196 ```
    197 
    198 </section>
    199 
    200 <!-- /.examples -->
    201 
    202 <!-- 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. -->
    203 
    204 <section class="references">
    205 
    206 </section>
    207 
    208 <!-- /.references -->
    209 
    210 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    211 
    212 <section class="links">
    213 
    214 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    215 
    216 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    217 
    218 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
    219 
    220 </section>
    221 
    222 <!-- /.links -->