|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2023 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 | +# nditerValues |
| 22 | + |
| 23 | +> Create an iterator which returns individual elements from a provided [`ndarray`][@stdlib/ndarray/ctor]. |
| 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 nditerValues = require( '@stdlib/ndarray/iter/values' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### nditerValues( x\[, options] ) |
| 44 | + |
| 45 | +Returns an iterator which returns individual elements from a provided [`ndarray`][@stdlib/ndarray/ctor]. |
| 46 | + |
| 47 | +```javascript |
| 48 | +var array = require( '@stdlib/ndarray/array' ); |
| 49 | + |
| 50 | +var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ] ); |
| 51 | +// returns <ndarray> |
| 52 | + |
| 53 | +var iter = nditerValues( x ); |
| 54 | + |
| 55 | +var v = iter.next().value; |
| 56 | +// returns 1 |
| 57 | + |
| 58 | +v = iter.next().value; |
| 59 | +// returns 2 |
| 60 | + |
| 61 | +v = iter.next().value; |
| 62 | +// returns 3 |
| 63 | + |
| 64 | +// ... |
| 65 | +``` |
| 66 | + |
| 67 | +The function accepts the following `options`: |
| 68 | + |
| 69 | +- **order**: index iteration order. By default, the returned [iterator][mdn-iterator-protocol] returns values according to the layout order of the provided array. Accordingly, for row-major input arrays, the last dimension indices increment fastest. For column-major input arrays, the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manor, regardless of the input array's layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input array may, in some circumstances, result in performance degradation due to cache misses. Must be either `'row-major'` or `'column-major'`. |
| 70 | + |
| 71 | +By default, the iterator iterates according to the layout order of the input [`ndarray`][@stdlib/ndarray/ctor]. To iterate according to a specified order, set the `order` option. |
| 72 | + |
| 73 | +```javascript |
| 74 | +var array = require( '@stdlib/ndarray/array' ); |
| 75 | + |
| 76 | +var x = array( [ [ [ 1, 2 ], [ 3, 4 ] ], [ [ 5, 6 ], [ 7, 8 ] ] ], { |
| 77 | + 'order': 'row-major' |
| 78 | +}); |
| 79 | +// returns <ndarray> |
| 80 | + |
| 81 | +var iter = nditerValues( x, { |
| 82 | + 'order': 'column-major' |
| 83 | +}); |
| 84 | + |
| 85 | +var v = iter.next().value; |
| 86 | +// returns 1 |
| 87 | + |
| 88 | +v = iter.next().value; |
| 89 | +// returns 5 |
| 90 | + |
| 91 | +v = iter.next().value; |
| 92 | +// returns 3 |
| 93 | + |
| 94 | +// ... |
| 95 | +``` |
| 96 | + |
| 97 | +The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: |
| 98 | + |
| 99 | +- **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][mdn-iterator-protocol] is finished. |
| 100 | +- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. |
| 101 | + |
| 102 | +</section> |
| 103 | + |
| 104 | +<!-- /.usage --> |
| 105 | + |
| 106 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 107 | + |
| 108 | +<section class="notes"> |
| 109 | + |
| 110 | +## Notes |
| 111 | + |
| 112 | +- If an environment supports `Symbol.iterator`, the returned iterator is iterable. |
| 113 | +- A returned iterator does **not** copy a provided [`ndarray`][@stdlib/ndarray/ctor]. To ensure iterable reproducibility, copy the input [`ndarray`][@stdlib/ndarray/ctor] **before** creating an iterator. Otherwise, any changes to the contents of input [`ndarray`][@stdlib/ndarray/ctor] will be reflected in the returned iterator. |
| 114 | +- In environments supporting `Symbol.iterator`, the function **explicitly** does **not** invoke an ndarray's `@@iterator` method, regardless of whether this method is defined. |
| 115 | + |
| 116 | +</section> |
| 117 | + |
| 118 | +<!-- /.notes --> |
| 119 | + |
| 120 | +<!-- Package usage examples. --> |
| 121 | + |
| 122 | +<section class="examples"> |
| 123 | + |
| 124 | +## Examples |
| 125 | + |
| 126 | +<!-- eslint no-undef: "error" --> |
| 127 | + |
| 128 | +```javascript |
| 129 | +var array = require( '@stdlib/ndarray/array' ); |
| 130 | +var zeroTo = require( '@stdlib/array/base/zero-to' ); |
| 131 | +var nditerValues = require( '@stdlib/ndarray/iter/values' ); |
| 132 | + |
| 133 | +// Define an input array: |
| 134 | +var x = array( zeroTo( 27 ), { |
| 135 | + 'shape': [ 3, 3, 3 ] |
| 136 | +}); |
| 137 | + |
| 138 | +// Create an iterator for returning individual elements: |
| 139 | +var it = nditerValues( x ); |
| 140 | + |
| 141 | +// Perform manual iteration... |
| 142 | +var v; |
| 143 | +while ( true ) { |
| 144 | + v = it.next(); |
| 145 | + if ( v.done ) { |
| 146 | + break; |
| 147 | + } |
| 148 | + console.log( v.value ); |
| 149 | +} |
| 150 | +``` |
| 151 | + |
| 152 | +</section> |
| 153 | + |
| 154 | +<!-- /.examples --> |
| 155 | + |
| 156 | +<!-- 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. --> |
| 157 | + |
| 158 | +<section class="references"> |
| 159 | + |
| 160 | +</section> |
| 161 | + |
| 162 | +<!-- /.references --> |
| 163 | + |
| 164 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 165 | + |
| 166 | +<section class="related"> |
| 167 | + |
| 168 | +</section> |
| 169 | + |
| 170 | +<!-- /.related --> |
| 171 | + |
| 172 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 173 | + |
| 174 | +<section class="links"> |
| 175 | + |
| 176 | +[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol |
| 177 | + |
| 178 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib |
| 179 | + |
| 180 | +</section> |
| 181 | + |
| 182 | +<!-- /.links --> |
0 commit comments