|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2019 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 | +# iterPipeline |
| 22 | + |
| 23 | +> Create an [iterator][mdn-iterator-protocol] pipeline. |
| 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 iterPipeline = require( '@stdlib/iter/pipeline' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### iterPipeline( iterFcn0\[, ...iterFcn] ) |
| 44 | + |
| 45 | +Returns an [iterator][mdn-iterator-protocol] pipeline. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 49 | +var iterThunk = require( '@stdlib/iter/pipeline-thunk' ); |
| 50 | +var iterHead = require( '@stdlib/iter/head' ); |
| 51 | +var iterSome = require( '@stdlib/iter/some' ); |
| 52 | + |
| 53 | +// Convert iterator functions to unary functions which accept an iterator: |
| 54 | +var it1 = iterThunk( iterHead, 5 ); |
| 55 | +var it2 = iterThunk( iterSome, 3 ); |
| 56 | + |
| 57 | +// Create an iterator pipeline: |
| 58 | +var p = iterPipeline( it1, it2 ); |
| 59 | + |
| 60 | +// Create a source iterator: |
| 61 | +var arr = array2iterator( [ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1 ] ); |
| 62 | + |
| 63 | +// Provide the source iterator to our iterator pipeline: |
| 64 | +var bool = p( arr ); |
| 65 | +// returns true |
| 66 | + |
| 67 | +// Create a new source iterator: |
| 68 | +arr = array2iterator( [ 0, 0, 1, 0, 1, 0, 0, 0, 1, 1 ] ); |
| 69 | + |
| 70 | +// Run the pipeline for the new source iterator: |
| 71 | +bool = p( arr ); |
| 72 | +// returns false |
| 73 | +``` |
| 74 | + |
| 75 | +The function accepts [iterator][mdn-iterator-protocol] functions provided as separate arguments **or** as a single argument consisting of an array of [iterator][mdn-iterator-protocol] functions. |
| 76 | + |
| 77 | +```javascript |
| 78 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 79 | +var iterThunk = require( '@stdlib/iter/pipeline-thunk' ); |
| 80 | +var iterHead = require( '@stdlib/iter/head' ); |
| 81 | +var iterSome = require( '@stdlib/iter/some' ); |
| 82 | + |
| 83 | +var it1 = iterThunk( iterHead, 5 ); |
| 84 | +var it2 = iterThunk( iterSome, 3 ); |
| 85 | + |
| 86 | +var p = iterPipeline( [ it1, it2 ] ); |
| 87 | + |
| 88 | +var arr = array2iterator( [ 0, 0, 1, 1, 1, 0, 0, 0, 1, 1 ] ); |
| 89 | +var bool = p( arr ); |
| 90 | +// returns true |
| 91 | + |
| 92 | +arr = array2iterator( [ 0, 0, 1, 0, 1, 0, 0, 0, 1, 1 ] ); |
| 93 | +bool = p( arr ); |
| 94 | +// returns false |
| 95 | +``` |
| 96 | + |
| 97 | +</section> |
| 98 | + |
| 99 | +<!-- /.usage --> |
| 100 | + |
| 101 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 102 | + |
| 103 | +<section class="notes"> |
| 104 | + |
| 105 | +## Notes |
| 106 | + |
| 107 | +- Within the context of an [iterator][mdn-iterator-protocol] pipeline (as defined by this function), an [iterator][mdn-iterator-protocol] function is defined as a **unary** function which accepts an [iterator][mdn-iterator-protocol] as its **only** argument. |
| 108 | +- Each [iterator][mdn-iterator-protocol] function, except the last [iterator][mdn-iterator-protocol] function, within an [iterator][mdn-iterator-protocol] pipeline must return an [iterator][mdn-iterator-protocol]. |
| 109 | +- Starting from the left, each returned [iterator][mdn-iterator-protocol] is passed to the next [iterator][mdn-iterator-protocol] function. |
| 110 | +- The result of the last [iterator][mdn-iterator-protocol] function is the result of the pipeline. |
| 111 | + |
| 112 | +</section> |
| 113 | + |
| 114 | +<!-- /.notes --> |
| 115 | + |
| 116 | +<!-- Package usage examples. --> |
| 117 | + |
| 118 | +<section class="examples"> |
| 119 | + |
| 120 | +## Examples |
| 121 | + |
| 122 | +<!-- eslint-disable function-paren-newline --> |
| 123 | + |
| 124 | +<!-- eslint no-undef: "error" --> |
| 125 | + |
| 126 | +```javascript |
| 127 | +var randu = require( '@stdlib/random/iter/randu' ); |
| 128 | +var iterHead = require( '@stdlib/iter/head' ); |
| 129 | +var iterMap = require( '@stdlib/iter/map' ); |
| 130 | +var iterSome = require( '@stdlib/iter/some' ); |
| 131 | +var iterThunk = require( '@stdlib/iter/pipeline-thunk' ); |
| 132 | +var iterPipeline = require( '@stdlib/iter/pipeline' ); |
| 133 | + |
| 134 | +function threshold( r ) { |
| 135 | + return ( r > 0.95 ); |
| 136 | +} |
| 137 | + |
| 138 | +// Create a pipeline which tests whether at least 5% of values exceed a threshold: |
| 139 | +var p = iterPipeline( |
| 140 | + // Apply a threshold to iterated values: |
| 141 | + iterThunk( iterMap, threshold ), |
| 142 | + |
| 143 | + // Limit the sequence to 100 values: |
| 144 | + iterThunk( iterHead, 100 ), |
| 145 | + |
| 146 | + // Test whether at least 5 values exceed the threshold: |
| 147 | + iterThunk( iterSome, 5 ) |
| 148 | +); |
| 149 | + |
| 150 | +// Define the number of random number sequences to analyze: |
| 151 | +var N = 100; |
| 152 | + |
| 153 | +// Initialize a counter for sequences satisfying the 5% threshold: |
| 154 | +var count = 0; |
| 155 | + |
| 156 | +// Perform analysis... |
| 157 | +var bool; |
| 158 | +var i; |
| 159 | +for ( i = 0; i < N; i++ ) { |
| 160 | + bool = p( randu() ); |
| 161 | + if ( bool ) { |
| 162 | + count += 1; |
| 163 | + } |
| 164 | + console.log( bool ); |
| 165 | +} |
| 166 | +console.log( '%d of %d', count, N ); |
| 167 | +``` |
| 168 | + |
| 169 | +</section> |
| 170 | + |
| 171 | +<!-- /.examples --> |
| 172 | + |
| 173 | +<!-- 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. --> |
| 174 | + |
| 175 | +<section class="references"> |
| 176 | + |
| 177 | +</section> |
| 178 | + |
| 179 | +<!-- /.references --> |
| 180 | + |
| 181 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 182 | + |
| 183 | +<section class="links"> |
| 184 | + |
| 185 | +[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol |
| 186 | + |
| 187 | +</section> |
| 188 | + |
| 189 | +<!-- /.links --> |
0 commit comments