|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2024 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 | +# forEach |
| 22 | + |
| 23 | +> Invoke a callback function once for each [ndarray][@stdlib/ndarray/ctor] element. |
| 24 | +
|
| 25 | +<section class="intro"> |
| 26 | + |
| 27 | +</section> |
| 28 | + |
| 29 | +<!-- /.intro --> |
| 30 | + |
| 31 | +<section class="usage"> |
| 32 | + |
| 33 | +## Usage |
| 34 | + |
| 35 | +```javascript |
| 36 | +var forEach = require( '@stdlib/ndarray/for-each' ); |
| 37 | +``` |
| 38 | + |
| 39 | +#### forEach( x, fcn\[, thisArg] ) |
| 40 | + |
| 41 | +Invokes a callback function once for each [ndarray][@stdlib/ndarray/ctor] element. |
| 42 | + |
| 43 | +<!-- eslint-disable max-len --> |
| 44 | + |
| 45 | +```javascript |
| 46 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 47 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 48 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 49 | +var log = require( '@stdlib/console/log' ); |
| 50 | + |
| 51 | +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 52 | +var shape = [ 2, 3 ]; |
| 53 | +var strides = [ 6, 1 ]; |
| 54 | +var offset = 1; |
| 55 | + |
| 56 | +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); |
| 57 | +// returns <ndarray> |
| 58 | + |
| 59 | +forEach( x, naryFunction( log, 1 ) ); |
| 60 | +``` |
| 61 | + |
| 62 | +The function accepts the following arguments: |
| 63 | + |
| 64 | +- **x**: input [ndarray][@stdlib/ndarray/ctor]. |
| 65 | +- **fcn**: callback to apply. |
| 66 | +- **thisArg**: callback execution context _(optional)_. |
| 67 | + |
| 68 | +To set the callback function execution context, provide a `thisArg`. |
| 69 | + |
| 70 | +<!-- eslint-disable no-invalid-this, max-len --> |
| 71 | + |
| 72 | +```javascript |
| 73 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 74 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 75 | + |
| 76 | +function accumulate( z ) { |
| 77 | + this.sum += z; |
| 78 | +} |
| 79 | + |
| 80 | +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); |
| 81 | +var shape = [ 2, 3 ]; |
| 82 | +var strides = [ 6, 1 ]; |
| 83 | +var offset = 1; |
| 84 | +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); |
| 85 | +// returns <ndarray> |
| 86 | + |
| 87 | +var ctx = { |
| 88 | + 'sum': 0 |
| 89 | +}; |
| 90 | + |
| 91 | +forEach( x, accumulate, ctx ); |
| 92 | +var sum = ctx.sum; |
| 93 | +// returns 36 |
| 94 | +``` |
| 95 | + |
| 96 | +The callback function is provided the following arguments: |
| 97 | + |
| 98 | +- **value**: current array element. |
| 99 | +- **indices**: current array element indices. |
| 100 | +- **arr**: the input [ndarray][@stdlib/ndarray/ctor]. |
| 101 | + |
| 102 | +</section> |
| 103 | + |
| 104 | +<!-- /.usage --> |
| 105 | + |
| 106 | +<section class="notes"> |
| 107 | + |
| 108 | +## Notes |
| 109 | + |
| 110 | +- For very high-dimensional ndarrays which are non-contiguous, one should consider copying the underlying data to contiguous memory before applying a callback function in order to achieve better performance. |
| 111 | + |
| 112 | +</section> |
| 113 | + |
| 114 | +<!-- /.notes --> |
| 115 | + |
| 116 | +<section class="examples"> |
| 117 | + |
| 118 | +## Examples |
| 119 | + |
| 120 | +<!-- eslint no-undef: "error" --> |
| 121 | + |
| 122 | +```javascript |
| 123 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 124 | +var ndarray = require( '@stdlib/ndarray/ctor' ); |
| 125 | +var ndarray2array = require( '@stdlib/ndarray/to-array' ); |
| 126 | +var naryFunction = require( '@stdlib/utils/nary-function' ); |
| 127 | +var log = require( '@stdlib/console/log' ); |
| 128 | +var forEach = require( '@stdlib/ndarray/for-each' ); |
| 129 | + |
| 130 | +var buffer = discreteUniform( 10, -100, 100, { |
| 131 | + 'dtype': 'generic' |
| 132 | +}); |
| 133 | +var shape = [ 5, 2 ]; |
| 134 | +var strides = [ 2, 1 ]; |
| 135 | +var offset = 0; |
| 136 | +var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); |
| 137 | + |
| 138 | +log( ndarray2array( x ) ); |
| 139 | +forEach( x, naryFunction( log, 2 ) ); |
| 140 | +``` |
| 141 | + |
| 142 | +</section> |
| 143 | + |
| 144 | +<!-- /.examples --> |
| 145 | + |
| 146 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 147 | + |
| 148 | +<section class="related"> |
| 149 | + |
| 150 | +</section> |
| 151 | + |
| 152 | +<!-- /.related --> |
| 153 | + |
| 154 | +<section class="links"> |
| 155 | + |
| 156 | +[@stdlib/ndarray/ctor]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/ctor |
| 157 | + |
| 158 | +<!-- <related-links> --> |
| 159 | + |
| 160 | +<!-- </related-links> --> |
| 161 | + |
| 162 | +</section> |
| 163 | + |
| 164 | +<!-- /.links --> |
0 commit comments