time-to-botec

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

README.md (6820B)


      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 # Iterator Stream
     22 
     23 > Create a [readable stream][readable-stream] from an [iterator][mdn-iterator-protocol].
     24 
     25 <section class="usage">
     26 
     27 ## Usage
     28 
     29 ```javascript
     30 var iteratorStream = require( '@stdlib/streams/node/from-iterator' );
     31 ```
     32 
     33 <a name="iterator-stream"></a>
     34 
     35 #### iteratorStream( iterator\[, options] )
     36 
     37 Returns a [readable stream][readable-stream] from an [iterator][mdn-iterator-protocol].
     38 
     39 ```javascript
     40 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     41 var randu = require( '@stdlib/random/iter/randu' );
     42 
     43 var iStream;
     44 var stream;
     45 
     46 function log( chunk, idx ) {
     47     console.log( chunk.toString() );
     48     if ( idx === 10 ) {
     49         stream.destroy();
     50     }
     51 }
     52 
     53 stream = iteratorStream( randu() );
     54 iStream = inspectStream( log );
     55 
     56 stream.pipe( iStream );
     57 ```
     58 
     59 The function accepts the following `options`:
     60 
     61 -   **objectMode**: specifies whether a [stream][stream] should operate in [objectMode][object-mode]. Default: `false`.
     62 -   **encoding**: specifies how `Buffer` objects should be decoded to `strings`. Default: `null`.
     63 -   **highWaterMark**: specifies the maximum number of bytes to store in an internal buffer before pausing iteration.
     64 -   **sep**: separator used to join streamed data. This option is only applicable when a stream is **not** in [objectMode][object-mode]. Default: `'\n'`.
     65 -   **serialize**: custom serialization function. This option is only applicable when a stream is **not** in [objectMode][object-mode].
     66 
     67 To set [stream][stream] `options`,
     68 
     69 ```javascript
     70 var randu = require( '@stdlib/random/iter/randu' );
     71 
     72 var opts = {
     73     'objectMode': true,
     74     'encoding': 'utf8',
     75     'highWaterMark': 64
     76 };
     77 
     78 var stream = iteratorStream( randu(), opts );
     79 ```
     80 
     81 By default, when not operating in [objectMode][object-mode], a returned [stream][stream] delineates iterated values using a newline character. To specify an alternative separator, set the `sep` option.
     82 
     83 ```javascript
     84 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
     85 var randu = require( '@stdlib/random/iter/randu' );
     86 
     87 function log( chunk ) {
     88     console.log( chunk.toString() );
     89 }
     90 
     91 var it = randu({
     92     'iter': 10
     93 });
     94 
     95 var stream = iteratorStream( it, {
     96     'sep': ','
     97 });
     98 
     99 var iStream = inspectStream( log );
    100 
    101 stream.pipe( iStream );
    102 ```
    103 
    104 By default, when not operating in [objectMode][object-mode], a returned [stream][stream] serializes iterated values as JSON strings. To specify custom serialization behavior (either to a `string` or `Buffer`), set the `serialize` option.
    105 
    106 ```javascript
    107 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    108 var randu = require( '@stdlib/random/iter/randu' );
    109 
    110 function serialize( v ) {
    111     return 'r::' + v.toString();
    112 }
    113 
    114 function log( chunk ) {
    115     console.log( chunk.toString() );
    116 }
    117 
    118 var it = randu({
    119     'iter': 10
    120 });
    121 
    122 var stream = iteratorStream( it, {
    123     'serialize': serialize
    124 });
    125 
    126 var iStream = inspectStream( log );
    127 
    128 stream.pipe( iStream );
    129 ```
    130 
    131 * * *
    132 
    133 #### iteratorStream.factory( \[options] )
    134 
    135 Returns a `function` for creating [readable streams][readable-stream] from [iterators][mdn-iterator-protocol].
    136 
    137 ```javascript
    138 var randu = require( '@stdlib/random/iter/randu' );
    139 
    140 var opts = {
    141     'objectMode': true,
    142     'encoding': 'utf8',
    143     'highWaterMark': 64
    144 };
    145 
    146 var createStream = iteratorStream.factory( opts );
    147 
    148 var stream1 = createStream( randu() );
    149 var stream2 = createStream( randu() );
    150 // ...
    151 ```
    152 
    153 The method accepts the same `options` as [`iteratorStream()`](#iterator-stream).
    154 
    155 * * *
    156 
    157 #### iteratorStream.objectMode( iterator\[, options] )
    158 
    159 This method is a convenience function to create [streams][stream] which **always** operate in [objectMode][object-mode].
    160 
    161 ```javascript
    162 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    163 var randu = require( '@stdlib/random/iter/randu' );
    164 
    165 function log( v ) {
    166     console.log( v );
    167 }
    168 
    169 var opts = {
    170     'iter': 10
    171 };
    172 var stream = iteratorStream.objectMode( randu( opts ) );
    173 
    174 opts = {
    175     'objectMode': true
    176 };
    177 var iStream = inspectStream( opts, log );
    178 
    179 stream.pipe( iStream );
    180 ```
    181 
    182 This method accepts the same `options` as [`iteratorStream()`](#iterator-stream); however, the method will **always** override the [`objectMode`][object-mode] option in `options`.
    183 
    184 </section>
    185 
    186 <!-- /.usage -->
    187 
    188 * * *
    189 
    190 <section class="notes">
    191 
    192 ## Notes
    193 
    194 -   In [`objectMode`][object-mode], `null` is a reserved value. If an [iterator][mdn-iterator-protocol] generates `null` values (e.g., as a means to encode missing values), the stream will prematurely end. Consider an alternative encoding or explicitly map `null` values to a different value by wrapping the provided [iterator][mdn-iterator-protocol] with another [iterator][mdn-iterator-protocol].
    195 -   In binary mode, if an [iterator][mdn-iterator-protocol] generates `undefined` values, the stream will emit an error. Consider providing a custom serialization function or explicitly map `undefined` values to a different value by wrapping the provided [iterator][mdn-iterator-protocol] with another [iterator][mdn-iterator-protocol].
    196 
    197 </section>
    198 
    199 <!-- /.notes -->
    200 
    201 * * *
    202 
    203 <section class="examples">
    204 
    205 ## Examples
    206 
    207 <!-- eslint no-undef: "error" -->
    208 
    209 ```javascript
    210 var inspectStream = require( '@stdlib/streams/node/inspect-sink' );
    211 var randu = require( '@stdlib/random/iter/randu' );
    212 var iteratorStream = require( '@stdlib/streams/node/from-iterator' );
    213 
    214 function log( v ) {
    215     console.log( v.toString() );
    216 }
    217 
    218 // Create an iterator which generates uniformly distributed pseudorandom numbers:
    219 var opts = {
    220     'iter': 10
    221 };
    222 var it = randu( opts );
    223 
    224 // Convert the iterator to a stream:
    225 opts = {
    226     'objectMode': true
    227 };
    228 var stream = iteratorStream( it, opts );
    229 
    230 // Create a writable stream for inspecting stream data:
    231 opts = {
    232     'objectMode': true
    233 };
    234 var iStream = inspectStream( opts, log );
    235 
    236 // Begin data flow:
    237 stream.pipe( iStream );
    238 ```
    239 
    240 </section>
    241 
    242 <!-- /.examples -->
    243 
    244 <section class="links">
    245 
    246 [stream]: https://nodejs.org/api/stream.html
    247 
    248 [object-mode]: https://nodejs.org/api/stream.html#stream_object_mode
    249 
    250 [readable-stream]: https://nodejs.org/api/stream.html
    251 
    252 [mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
    253 
    254 </section>
    255 
    256 <!-- /.links -->