time-to-botec

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

README.md (6736B)


      1 <!--
      2 
      3 @license Apache-2.0
      4 
      5 Copyright (c) 2020 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 # iterMap2
     22 
     23 > Create an [iterator][mdn-iterator-protocol] which invokes a binary function accepting numeric arguments for each iterated value.
     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 iterMap2 = require( '@stdlib/math/iter/tools/map2' );
     41 ```
     42 
     43 #### iterMap2( iter0, iter1, fcn\[, options] )
     44 
     45 Returns an [iterator][mdn-iterator-protocol] which invokes a binary `function` accepting numeric arguments for each iterated value.
     46 
     47 ```javascript
     48 var array2iterator = require( '@stdlib/array/to-iterator' );
     49 var copysign = require( '@stdlib/math/base/special/copysign' );
     50 
     51 var it1 = array2iterator( [ 1.0, 2.0, 3.0, 4.0 ] );
     52 var it2 = array2iterator( [ 1.0, -1.0, -1.0, 1.0 ] );
     53 
     54 var it = iterMap2( it1, it2, copysign );
     55 // returns <Object>
     56 
     57 var r = it.next().value;
     58 // returns 1.0
     59 
     60 r = it.next().value;
     61 // returns -2.0
     62 
     63 r = it.next().value;
     64 // returns -3.0
     65 
     66 // ...
     67 ```
     68 
     69 The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
     70 
     71 -   **next**: function which returns an [iterator][mdn-iterator-protocol] protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished.
     72 -   **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
     73 
     74 The invoked `function` is provided two arguments:
     75 
     76 -   `x`: iterated value from first input [iterator][mdn-iterator-protocol].
     77 -   `y`: iterated value from second input [iterator][mdn-iterator-protocol].
     78 
     79 ```javascript
     80 var array2iterator = require( '@stdlib/array/to-iterator' );
     81 
     82 function fcn( x, y ) {
     83     return x + y + 10;
     84 }
     85 
     86 var it1 = array2iterator( [ 1, 2, 3, 4 ] );
     87 var it2 = array2iterator( [ 1, 2, 3, 4 ] );
     88 
     89 var it = iterMap2( it1, it2, fcn );
     90 // returns <Object>
     91 
     92 var r = it.next().value;
     93 // returns 12
     94 
     95 r = it.next().value;
     96 // returns 14
     97 
     98 r = it.next().value;
     99 // returns 16
    100 
    101 // ...
    102 ```
    103 
    104 The function supports the following `options`:
    105 
    106 -   **invalid**: return value when an input [iterator][mdn-iterator-protocol] yields a non-numeric value. Default: `NaN`.
    107 
    108 By default, the function returns an [iterator][mdn-iterator-protocol] which returns `NaN` when an input [iterator][mdn-iterator-protocol] yields a non-numeric value. To specify a different return value, set the `invalid` option.
    109 
    110 ```javascript
    111 var array2iterator = require( '@stdlib/array/to-iterator' );
    112 var copysign = require( '@stdlib/math/base/special/copysign' );
    113 
    114 var it1 = array2iterator( [ '1.0', '2.0', '3.0' ] );
    115 var it2 = array2iterator( [ 1.0, 2.0, 3.0 ] );
    116 
    117 var opts = {
    118     'invalid': null
    119 };
    120 var it = iterMap2( it1, it2, copysign, opts );
    121 // returns <Object>
    122 
    123 var v = it.next().value;
    124 // returns null
    125 
    126 v = it.next().value;
    127 // returns null
    128 
    129 // ...
    130 ```
    131 
    132 If provided a numeric value as an [`iterator`][mdn-iterator-protocol] argument, the value is broadcast as an **infinite** [iterator][mdn-iterator-protocol] which **always** returns the provided value.
    133 
    134 ```javascript
    135 var array2iterator = require( '@stdlib/array/to-iterator' );
    136 var copysign = require( '@stdlib/math/base/special/copysign' );
    137 
    138 var it1 = array2iterator( [ 1.0, 2.0 ] );
    139 
    140 var it = iterMap2( it1, -4.0, copysign );
    141 // returns <Object>
    142 
    143 var v = it.next().value;
    144 // returns -1.0
    145 
    146 v = it.next().value;
    147 // returns -2.0
    148 
    149 var bool = it.next().done;
    150 // returns true
    151 ```
    152 
    153 </section>
    154 
    155 <!-- /.usage -->
    156 
    157 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    158 
    159 <section class="notes">
    160 
    161 ## Notes
    162 
    163 -   If an iterated value is non-numeric (including `NaN`), the returned [iterator][mdn-iterator-protocol] returns `NaN`. If non-numeric iterated values are possible, you are advised to provide an [`iterator`][mdn-iterator-protocol] which type checks and handles non-numeric values accordingly.
    164 -   The length of the returned [iterator][mdn-iterator-protocol] is equal to the length of the shortest provided [iterator][mdn-iterator-protocol]. In other words, the returned [iterator][mdn-iterator-protocol] ends once **one** of the provided [iterators][mdn-iterator-protocol] ends.
    165 -   If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable.
    166 
    167 </section>
    168 
    169 <!-- /.notes -->
    170 
    171 <!-- Package usage examples. -->
    172 
    173 <section class="examples">
    174 
    175 ## Examples
    176 
    177 <!-- eslint no-undef: "error" -->
    178 
    179 ```javascript
    180 var randu = require( '@stdlib/random/iter/randu' );
    181 var uniform = require( '@stdlib/random/iter/uniform' );
    182 var copysign = require( '@stdlib/math/base/special/copysign' );
    183 var iterMap2 = require( '@stdlib/math/iter/tools/map2' );
    184 
    185 // Create seeded iterators for generating pseudorandom numbers:
    186 var rand1 = randu({
    187     'seed': 1234,
    188     'iter': 10
    189 });
    190 
    191 var rand2 = uniform( -1.0, 1.0, {
    192     'seed': 1234,
    193     'iter': 10
    194 });
    195 
    196 // Create an iterator which consumes the pseudorandom number iterators:
    197 var it = iterMap2( rand1, rand2, copysign );
    198 
    199 // Perform manual iteration...
    200 var r;
    201 while ( true ) {
    202     r = it.next();
    203     if ( r.done ) {
    204         break;
    205     }
    206     console.log( r.value );
    207 }
    208 ```
    209 
    210 </section>
    211 
    212 <!-- /.examples -->
    213 
    214 <!-- 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. -->
    215 
    216 <section class="references">
    217 
    218 </section>
    219 
    220 <!-- /.references -->
    221 
    222 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    223 
    224 <section class="links">
    225 
    226 [mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
    227 
    228 </section>
    229 
    230 <!-- /.links -->