|
| 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 | +# iterStridedBy |
| 22 | + |
| 23 | +> Create an [iterator][mdn-iterator-protocol] which steps according to a provided callback function. |
| 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 iterStridedBy = require( '@stdlib/iter/strided-by' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### iterStridedBy( iterator, fcn\[, offset\[, eager]]\[, thisArg] ) |
| 44 | + |
| 45 | +Returns an [iterator][mdn-iterator-protocol] which steps according to a provided callback function. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 49 | + |
| 50 | +function stride( v, i ) { |
| 51 | + return (i % 10) + 1; |
| 52 | +} |
| 53 | + |
| 54 | +var arr = array2iterator( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ); |
| 55 | +var it = iterStridedBy( arr, stride ); |
| 56 | +// returns <Object> |
| 57 | + |
| 58 | +var r = it.next().value; |
| 59 | +// returns 1 |
| 60 | + |
| 61 | +r = it.next().value; |
| 62 | +// returns 2 |
| 63 | + |
| 64 | +r = it.next().value; |
| 65 | +// returns 4 |
| 66 | + |
| 67 | +// ... |
| 68 | +``` |
| 69 | + |
| 70 | +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: |
| 71 | + |
| 72 | +- **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. |
| 73 | +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. |
| 74 | + |
| 75 | +The callback function is provided four arguments: |
| 76 | + |
| 77 | +- **value**: iterated value |
| 78 | +- **i**: source iteration index (zero-based) |
| 79 | +- **n**: iteration index (zero-based) |
| 80 | +- **curr**: current stride |
| 81 | + |
| 82 | +To set the callback execution context, provide a `thisArg`. |
| 83 | + |
| 84 | +<!-- eslint-disable no-invalid-this --> |
| 85 | + |
| 86 | +```javascript |
| 87 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 88 | + |
| 89 | +function stride( v, i ) { |
| 90 | + this.count += 1; |
| 91 | + return (i % 10) + 1; |
| 92 | +} |
| 93 | + |
| 94 | +var ctx = { |
| 95 | + 'count': 0 |
| 96 | +}; |
| 97 | + |
| 98 | +var arr = array2iterator( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ); |
| 99 | +var it = iterStridedBy( arr, stride, ctx ); |
| 100 | +// returns <Object> |
| 101 | + |
| 102 | +var v = it.next().value; |
| 103 | +// returns 1 |
| 104 | + |
| 105 | +v = it.next().value; |
| 106 | +// returns 2 |
| 107 | + |
| 108 | +v = it.next().value; |
| 109 | +// returns 4 |
| 110 | + |
| 111 | +v = it.next().value; |
| 112 | +// returns 8 |
| 113 | + |
| 114 | +var count = ctx.count; |
| 115 | +// returns 4 |
| 116 | +``` |
| 117 | + |
| 118 | +To skip the first `N` values of a provided [`iterator`][mdn-iterator-protocol], provide an `offset` argument. |
| 119 | + |
| 120 | +```javascript |
| 121 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 122 | + |
| 123 | +function stride( v, i ) { |
| 124 | + return (i % 10) + 1; |
| 125 | +} |
| 126 | + |
| 127 | +var arr = array2iterator( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ); |
| 128 | +var it = iterStridedBy( arr, stride, 1 ); |
| 129 | +// returns <Object> |
| 130 | + |
| 131 | +var r = it.next().value; |
| 132 | +// returns 2 |
| 133 | + |
| 134 | +r = it.next().value; |
| 135 | +// returns 4 |
| 136 | + |
| 137 | +r = it.next().value; |
| 138 | +// returns 8 |
| 139 | + |
| 140 | +// ... |
| 141 | +``` |
| 142 | + |
| 143 | +By default, the returned [iterator][mdn-iterator-protocol] defers consuming the first `N` input [`iterator`][mdn-iterator-protocol] values until the first value of the returned [iterator][mdn-iterator-protocol] is consumed. To eagerly advance the input [`iterator`][mdn-iterator-protocol], set the `eager` argument to `true`. |
| 144 | + |
| 145 | +```javascript |
| 146 | +var array2iterator = require( '@stdlib/array/to-iterator' ); |
| 147 | + |
| 148 | +function stride() { |
| 149 | + return 1; |
| 150 | +} |
| 151 | + |
| 152 | +var arr = array2iterator( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ); |
| 153 | +var it = iterStridedBy( arr, stride, 4, true ); |
| 154 | +// returns <Object> |
| 155 | + |
| 156 | +var r = it.next().value; |
| 157 | +// returns 5 |
| 158 | + |
| 159 | +r = it.next().value; |
| 160 | +// returns 6 |
| 161 | + |
| 162 | +r = it.next().value; |
| 163 | +// returns 7 |
| 164 | + |
| 165 | +// ... |
| 166 | +``` |
| 167 | + |
| 168 | +</section> |
| 169 | + |
| 170 | +<!-- /.usage --> |
| 171 | + |
| 172 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 173 | + |
| 174 | +<section class="notes"> |
| 175 | + |
| 176 | +## Notes |
| 177 | + |
| 178 | +- A callback function **must** return a **positive integer** value. |
| 179 | +- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable. |
| 180 | + |
| 181 | +</section> |
| 182 | + |
| 183 | +<!-- /.notes --> |
| 184 | + |
| 185 | +<!-- Package usage examples. --> |
| 186 | + |
| 187 | +<section class="examples"> |
| 188 | + |
| 189 | +## Examples |
| 190 | + |
| 191 | +<!-- eslint no-undef: "error" --> |
| 192 | + |
| 193 | +```javascript |
| 194 | +var randu = require( '@stdlib/random/iter/randu' ); |
| 195 | +var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory; |
| 196 | +var iterStridedBy = require( '@stdlib/iter/strided-by' ); |
| 197 | + |
| 198 | +// Create a seeded iterator for generating pseudorandom numbers: |
| 199 | +var rand = randu({ |
| 200 | + 'seed': 1234, |
| 201 | + 'iter': 10 |
| 202 | +}); |
| 203 | + |
| 204 | +// Create a PRNG for generating pseudorandom integers on the interval [1,10]: |
| 205 | +var randi = discreteUniform( 1, 10, { |
| 206 | + 'seed': 4321 |
| 207 | +}); |
| 208 | + |
| 209 | +// Create an iterator which randomly selects input iterator values: |
| 210 | +var it = iterStridedBy( rand, randi ); |
| 211 | + |
| 212 | +// Perform manual iteration... |
| 213 | +var r; |
| 214 | +while ( true ) { |
| 215 | + r = it.next(); |
| 216 | + if ( r.done ) { |
| 217 | + break; |
| 218 | + } |
| 219 | + console.log( r.value ); |
| 220 | +} |
| 221 | +``` |
| 222 | + |
| 223 | +</section> |
| 224 | + |
| 225 | +<!-- /.examples --> |
| 226 | + |
| 227 | +<!-- 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. --> |
| 228 | + |
| 229 | +<section class="references"> |
| 230 | + |
| 231 | +</section> |
| 232 | + |
| 233 | +<!-- /.references --> |
| 234 | + |
| 235 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 236 | + |
| 237 | +<section class="links"> |
| 238 | + |
| 239 | +[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol |
| 240 | + |
| 241 | +</section> |
| 242 | + |
| 243 | +<!-- /.links --> |
0 commit comments