time-to-botec

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

README.md (4563B)


      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 # iterator2array
     22 
     23 > Create (or fill) an array from an iterator.
     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 iterator2array = require( '@stdlib/array/from-iterator' );
     41 ```
     42 
     43 #### iterator2array( iterator\[, out]\[, mapFcn\[, thisArg]] )
     44 
     45 Creates (or fills) an array from an `iterator`.
     46 
     47 ```javascript
     48 var randu = require( '@stdlib/random/iter/randu' );
     49 
     50 var opts = {
     51     'iter': 10
     52 };
     53 
     54 var arr = iterator2array( randu( opts ) );
     55 // returns <Array>
     56 ```
     57 
     58 By default, the function creates and fills a generic `array`. To fill an array-like `object`, provide an `out` argument.
     59 
     60 ```javascript
     61 var Float64Array = require( '@stdlib/array/float64' );
     62 var randu = require( '@stdlib/random/iter/randu' );
     63 
     64 var out = new Float64Array( 10 );
     65 
     66 var arr = iterator2array( randu(), out );
     67 // returns <Float64Array>
     68 
     69 var bool = ( out === arr );
     70 // returns true
     71 ```
     72 
     73 To invoke a function for each iterated value, provide a callback function.
     74 
     75 ```javascript
     76 var array2iterator = require( '@stdlib/array/to-iterator' );
     77 
     78 function fcn( v ) {
     79     return v * 10.0;
     80 }
     81 
     82 var arr = iterator2array( array2iterator( [ 1, 2, 3, 4 ] ), fcn );
     83 // returns [ 10.0, 20.0, 30.0, 40.0 ]
     84 ```
     85 
     86 The invoked function is provided two arguments:
     87 
     88 -   `value`: iterated value
     89 -   `index`: iterated value index
     90 
     91 ```javascript
     92 var Float64Array = require( '@stdlib/array/float64' );
     93 var randu = require( '@stdlib/random/iter/randu' );
     94 
     95 function fcn( v, i ) {
     96     return v * (i+1);
     97 }
     98 
     99 var arr = iterator2array( randu(), new Float64Array( 10 ), fcn );
    100 // returns <Float64Array>
    101 ```
    102 
    103 To set the callback function execution context, provide a `thisArg`.
    104 
    105 ```javascript
    106 var Float64Array = require( '@stdlib/array/float64' );
    107 var randu = require( '@stdlib/random/iter/randu' );
    108 
    109 function fcn( v ) {
    110     this.count += 1;
    111     return v * 10.0;
    112 }
    113 
    114 var ctx = {
    115     'count': 0
    116 };
    117 
    118 var arr = iterator2array( randu(), new Float64Array( 10 ), fcn, ctx );
    119 // returns <Float64Array>
    120 
    121 var count = ctx.count;
    122 // returns 10
    123 ```
    124 
    125 </section>
    126 
    127 <!-- /.usage -->
    128 
    129 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    130 
    131 <section class="notes">
    132 
    133 ## Notes
    134 
    135 -   If provided an output array, the function fills the output array with iterated values.
    136 -   Iteration stops when an output array is full **or** an iterator finishes; whichever comes first.
    137 -   By providing an output typed array, one avoids the temporary memory allocation when using the built-in `TypedArray.from`.
    138 
    139 </section>
    140 
    141 <!-- /.notes -->
    142 
    143 <!-- Package usage examples. -->
    144 
    145 <section class="examples">
    146 
    147 ## Examples
    148 
    149 <!-- eslint no-undef: "error" -->
    150 
    151 ```javascript
    152 var Float64Array = require( '@stdlib/array/float64' );
    153 var randu = require( '@stdlib/random/iter/randu' );
    154 var iterator2array = require( '@stdlib/array/from-iterator' );
    155 
    156 var opts;
    157 var arr;
    158 var it;
    159 var i;
    160 
    161 function scale( v, i ) {
    162     return v * (i+1);
    163 }
    164 
    165 // Create an iterator for generating uniformly distributed pseudorandom numbers:
    166 opts = {
    167     'iter': 10
    168 };
    169 it = randu( opts );
    170 
    171 // Fill an array with scaled iterator values:
    172 arr = iterator2array( it, new Float64Array( opts.iter ), scale );
    173 
    174 for ( i = 0; i < arr.length; i++ ) {
    175     console.log( arr[ i ] );
    176 }
    177 ```
    178 
    179 </section>
    180 
    181 <!-- /.examples -->
    182 
    183 <!-- 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. -->
    184 
    185 <section class="references">
    186 
    187 </section>
    188 
    189 <!-- /.references -->
    190 
    191 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
    192 
    193 <section class="links">
    194 
    195 </section>
    196 
    197 <!-- /.links -->