time-to-botec

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

README.md (7002B)


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