time-to-botec

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

README.md (4958B)


      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 # anyBy
     22 
     23 > Test whether at least one element in a collection passes a test implemented by a predicate 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 anyBy = require( '@stdlib/utils/any-by' );
     41 ```
     42 
     43 #### anyBy( collection, predicate\[, thisArg ] )
     44 
     45 Tests whether at least one element in a `collection` passes a test implemented by a `predicate` function.
     46 
     47 ```javascript
     48 function isNegative( value ) {
     49     return ( value < 0 );
     50 }
     51 
     52 var arr = [ 1, 2, 3, 4, -1 ];
     53 
     54 var bool = anyBy( arr, isNegative );
     55 // returns true
     56 ```
     57 
     58 If a `predicate` function returns a truthy value, the function **immediately** returns `true`.
     59 
     60 ```javascript
     61 function isPositive( value ) {
     62     if ( value < 0 ) {
     63         throw new Error( 'should never reach this line' );
     64     }
     65     return ( value > 0 );
     66 }
     67 
     68 var arr = [ 1, -2, 3, 4 ];
     69 
     70 var bool = anyBy( arr, isPositive );
     71 // returns true
     72 ```
     73 
     74 The invoked `function` is provided three arguments:
     75 
     76 -   `value`: collection element
     77 -   `index`: collection index
     78 -   `collection`: input collection
     79 
     80 To set the function execution context, provide a `thisArg`.
     81 
     82 ```javascript
     83 function sum( value ) {
     84     this.sum += value;
     85     this.count += 1;
     86     return ( value < 0 );
     87 }
     88 
     89 var arr = [ 1, 2, 3, 4, -5 ];
     90 
     91 var context = {
     92     'sum': 0,
     93     'count': 0
     94 };
     95 
     96 var bool = anyBy( arr, sum, context );
     97 // returns true
     98 
     99 var mean = context.sum / context.count;
    100 // returns 1.0
    101 ```
    102 
    103 </section>
    104 
    105 <!-- /.usage -->
    106 
    107 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    108 
    109 <section class="notes">
    110 
    111 ## Notes
    112 
    113 -   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`).
    114 
    115 -   If provided an empty collection, the function returns `false`.
    116 
    117     ```javascript
    118     function alwaysTrue() {
    119         return true;
    120     }
    121     var bool = anyBy( [], alwaysTrue );
    122     // returns false
    123     ```
    124 
    125 -   The function differs from [`Array.prototype.some`][mdn-array-some] in the following ways:
    126 
    127     -   The function does **not** skip `undefined` elements.
    128 
    129         <!-- eslint-disable no-sparse-arrays, stdlib/doctest-marker -->
    130 
    131         ```javascript
    132         function log( value, index ) {
    133             console.log( '%s: %s', index, value );
    134             return ( value < 0 );
    135         }
    136 
    137         var arr = [ 1, , , 4, -1 ];
    138 
    139         var bool = anyBy( arr, log );
    140         /* =>
    141             0: 1
    142             1: undefined
    143             2: undefined
    144             3: 4
    145             4: -1
    146         */
    147         ```
    148 
    149     -   The function provides limited support for dynamic collections (i.e., collections whose `length` changes during execution).
    150 
    151 </section>
    152 
    153 <!-- /.notes -->
    154 
    155 <!-- Package usage examples. -->
    156 
    157 <section class="examples">
    158 
    159 ## Examples
    160 
    161 <!-- eslint no-undef: "error" -->
    162 
    163 ```javascript
    164 var randu = require( '@stdlib/random/base/randu' );
    165 var anyBy = require( '@stdlib/utils/any-by' );
    166 
    167 function threshold( value ) {
    168     return ( value > 0.95 );
    169 }
    170 
    171 var bool;
    172 var arr;
    173 var i;
    174 
    175 arr = [];
    176 for ( i = 0; i < 100; i++ ) {
    177     arr.push( randu() );
    178 }
    179 
    180 bool = anyBy( arr, threshold );
    181 // returns <boolean>
    182 ```
    183 
    184 </section>
    185 
    186 <!-- /.examples -->
    187 
    188 <!-- 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. -->
    189 
    190 <section class="references">
    191 
    192 </section>
    193 
    194 <!-- /.references -->
    195 
    196 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    197 
    198 <section class="links">
    199 
    200 [mdn-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
    201 
    202 [mdn-typed-array]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray
    203 
    204 [mdn-object]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object
    205 
    206 [mdn-array-some]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
    207 
    208 </section>
    209 
    210 <!-- /.links -->