time-to-botec

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

README.md (4445B)


      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 # tabulateBy
     22 
     23 > Generate a frequency table 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 tabulateBy = require( '@stdlib/utils/tabulate-by' );
     41 ```
     42 
     43 #### tabulateBy( collection\[, options,] indicator )
     44 
     45 Generates a frequency table according to an `indicator` function, i.e., a function which specifies how to categorize an element in the input `collection`.
     46 
     47 ```javascript
     48 function indicator( v ) {
     49     return v[ 0 ];
     50 }
     51 var arr = [ 'beep', 'boop', 'foo', 'beep' ];
     52 
     53 var out = tabulateBy( arr, indicator );
     54 // returns [ [ 'b', 3, 0.75 ], [ 'f', 1, 0.25 ] ]
     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 = tabulateBy( arr, indicator );
     70 // returns [ [ 'b', 3, 0.75 ], [ 'f', 1, 0.25 ] ]
     71 ```
     72 
     73 The function accepts the following `options`:
     74 
     75 -   `thisArg`: execution context.
     76 
     77 To set the `indicator` execution context, provide a `thisArg`.
     78 
     79 ```javascript
     80 function indicator( v ) {
     81     this.count += 1;
     82     return v[ 0 ];
     83 }
     84 var context = {
     85     'count': 0
     86 };
     87 var opts = {
     88     'thisArg': context
     89 };
     90 var arr = [ 'beep', 'boop', 'foo', 'bar' ];
     91 
     92 var out = tabulateBy( arr, opts, indicator );
     93 // returns [ [ 'b', 3, 0.75 ], [ 'f', 1, 0.25 ] ]
     94 
     95 console.log( context.count );
     96 // => 4
     97 ```
     98 
     99 The returned frequency table is an `array` of `arrays`. Each sub-array corresponds to a unique value in the input `collection` and is structured as follows:
    100 
    101 -   `0`: unique value
    102 -   `1`: value count
    103 -   `2`: frequency percentage
    104 
    105 </section>
    106 
    107 <!-- /.usage -->
    108 
    109 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    110 
    111 <section class="notes">
    112 
    113 ## Notes
    114 
    115 -   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`).
    116 
    117 </section>
    118 
    119 <!-- /.notes -->
    120 
    121 <!-- Package usage examples. -->
    122 
    123 <section class="examples">
    124 
    125 ## Examples
    126 
    127 <!-- eslint no-undef: "error" -->
    128 
    129 ```javascript
    130 var randu = require( '@stdlib/random/base/randu' );
    131 var floor = require( '@stdlib/math/base/special/floor' );
    132 var tabulateBy = require( '@stdlib/utils/tabulate-by' );
    133 
    134 var vals;
    135 var arr;
    136 var out;
    137 var i;
    138 var j;
    139 
    140 function indicator( value ) {
    141     return value[ 0 ];
    142 }
    143 
    144 vals = [ 'beep', 'boop', 'foo', 'bar', 'woot', 'woot' ];
    145 
    146 // Generate a random collection...
    147 arr = new Array( 100 );
    148 for ( i = 0; i < arr.length; i++ ) {
    149     j = floor( randu()*vals.length );
    150     arr[ i ] = vals[ j ];
    151 }
    152 
    153 // Generate a frequency table:
    154 out = tabulateBy( arr, indicator );
    155 console.log( out );
    156 ```
    157 
    158 </section>
    159 
    160 <!-- /.examples -->
    161 
    162 <!-- 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. -->
    163 
    164 <section class="references">
    165 
    166 </section>
    167 
    168 <!-- /.references -->
    169 
    170 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    171 
    172 <section class="links">
    173 
    174 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    175 
    176 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    177 
    178 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
    179 
    180 </section>
    181 
    182 <!-- /.links -->