time-to-botec

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

README.md (4866B)


      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 # group
     22 
     23 > Group values as arrays associated with distinct keys.
     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 group = require( '@stdlib/utils/group' );
     41 ```
     42 
     43 #### group( collection, \[options,] groups )
     44 
     45 Groups values as arrays associated with distinct keys.
     46 
     47 ```javascript
     48 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     49 var groups = [ 'b', 'b', 'f', 'b' ];
     50 
     51 var out = group( arr, groups );
     52 // returns { 'b': [ 'beep', 'boop', 'bar' ], 'f': [ 'foo' ] }
     53 ```
     54 
     55 The function accepts the following `options`:
     56 
     57 -   `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'`.
     58 
     59 By default, the function returns element values. To return element indices, set the `returns` option to `'indices'`.
     60 
     61 ```javascript
     62 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     63 var groups = [ 'b', 'b', 'f', 'b' ];
     64 
     65 var opts = {
     66     'returns': 'indices'
     67 };
     68 var out = group( arr, opts, groups );
     69 // returns { 'b': [ 0, 1, 3 ], 'f': [ 2 ] }
     70 ```
     71 
     72 To return index-element pairs, set the `returns` option to `'*'`.
     73 
     74 ```javascript
     75 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     76 var groups = [ 'b', 'b', 'f', 'b' ];
     77 
     78 var opts = {
     79     'returns': '*'
     80 };
     81 var out = group( arr, opts, groups );
     82 // returns { 'b': [ [ 0, 'beep' ], [ 1, 'boop' ], [ 3, 'bar' ] ], 'f': [ [ 2, 'foo' ] ] }
     83 ```
     84 
     85 </section>
     86 
     87 <!-- /.usage -->
     88 
     89 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
     90 
     91 <section class="notes">
     92 
     93 ## Notes
     94 
     95 -   Both `collection` and `groups` **must** be collections. 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`).
     96 
     97 -   Each value in `groups` should resolve to a value which can be serialized as an `object` key. As a counterexample,
     98 
     99     ```javascript
    100     var arr = [ 'beep', 'boop', 'foo', 'bar' ];
    101     var groups = [ {}, {}, {}, {} ];
    102 
    103     var out = group( arr, groups );
    104     // returns { '[object Object]': [ 'beep', 'boop', 'foo', 'bar' ] }
    105     ```
    106 
    107     while each "group" is unique, all collection elements resolve to the same group because each group identifier serializes to the same `string`. 
    108 
    109 </section>
    110 
    111 <!-- /.notes -->
    112 
    113 <!-- Package usage examples. -->
    114 
    115 <section class="examples">
    116 
    117 ## Examples
    118 
    119 <!-- eslint no-undef: "error" -->
    120 
    121 ```javascript
    122 var randu = require( '@stdlib/random/base/randu' );
    123 var floor = require( '@stdlib/math/base/special/floor' );
    124 var group = require( '@stdlib/utils/group' );
    125 
    126 var vals;
    127 var arr;
    128 var out;
    129 var g;
    130 var i;
    131 var j;
    132 
    133 vals = [ 'beep', 'boop', 'foo', 'bar', 'woot', 'woot' ];
    134 
    135 // Generate a random collection...
    136 arr = new Array( 100 );
    137 for ( i = 0; i < arr.length; i++ ) {
    138     j = floor( randu()*vals.length );
    139     arr[ i ] = vals[ j ];
    140 }
    141 
    142 // Randomly assign collection values to groups...
    143 g = new Array( arr.length );
    144 for ( i = 0; i < arr.length; i++ ) {
    145     g[ i ] = floor( randu()*vals.length );
    146 }
    147 
    148 // Compute the groups:
    149 out = group( arr, g );
    150 console.log( out );
    151 ```
    152 
    153 </section>
    154 
    155 <!-- /.examples -->
    156 
    157 <!-- 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. -->
    158 
    159 <section class="references">
    160 
    161 </section>
    162 
    163 <!-- /.references -->
    164 
    165 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    166 
    167 <section class="links">
    168 
    169 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    170 
    171 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    172 
    173 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
    174 
    175 </section>
    176 
    177 <!-- /.links -->