|
| 1 | +<!-- |
| 2 | +
|
| 3 | +@license Apache-2.0 |
| 4 | +
|
| 5 | +Copyright (c) 2025 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 | +# logEachMap |
| 22 | + |
| 23 | +> Insert array element values and the result of a callback function into a format string and print the result. |
| 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 logEachMap = require( '@stdlib/console/log-each-map' ); |
| 41 | +``` |
| 42 | + |
| 43 | +#### logEachMap( str\[, ...args], clbk\[, thisArg] ) |
| 44 | + |
| 45 | +Inserts array element values and the result of a callback function into a format string and prints the result. |
| 46 | + |
| 47 | +```javascript |
| 48 | +function add( a, b ) { |
| 49 | + return a + b; |
| 50 | +} |
| 51 | + |
| 52 | +var x = [ 1, 2, 3 ]; |
| 53 | +var y = [ 4, 5, 6 ]; |
| 54 | + |
| 55 | +logEachMap( '%d + %d = %d', x, y, add ); |
| 56 | +// e.g., => '1 + 4 = 5\n2 + 5 = 7\n3 + 6 = 9\n' |
| 57 | +``` |
| 58 | + |
| 59 | +The function accepts the following arguments: |
| 60 | + |
| 61 | +- **str**: format string. |
| 62 | +- **args**: input arrays _(optional)_. |
| 63 | +- **clbk**: callback function. |
| 64 | +- **thisArg**: callback execution context _(optional)_. |
| 65 | + |
| 66 | +To set the callback execution context, provide a `thisArg`. |
| 67 | + |
| 68 | +<!-- eslint-disable no-invalid-this --> |
| 69 | + |
| 70 | +```javascript |
| 71 | +function count( x, y ) { |
| 72 | + this.count += 1; |
| 73 | + return x + y; |
| 74 | +} |
| 75 | + |
| 76 | +var x = [ 1, 2, 3 ]; |
| 77 | +var y = [ 4, 5, 6 ]; |
| 78 | + |
| 79 | +var ctx = { |
| 80 | + 'count': 0 |
| 81 | +}; |
| 82 | + |
| 83 | +logEachMap( '%d + %d = %d', x, y, count, ctx ); |
| 84 | +// e.g., => '1 + 4 = 5\n2 + 5 = 7\n3 + 6 = 9\n' |
| 85 | + |
| 86 | +var v = ctx.count; |
| 87 | +// returns 3 |
| 88 | +``` |
| 89 | + |
| 90 | +If an interpolated argument is not an array-like object, the argument is broadcasted for each iteration. |
| 91 | + |
| 92 | +```javascript |
| 93 | +function multiply( x, y ) { |
| 94 | + return x * y; |
| 95 | +} |
| 96 | + |
| 97 | +var x = [ 1, 2, 3 ]; |
| 98 | +var y = 2; |
| 99 | + |
| 100 | +logEachMap( '%d * %d = %d', x, y, multiply ); |
| 101 | +// e.g., => '1 * 2 = 2\n2 * 2 = 4\n3 * 2 = 6\n' |
| 102 | +``` |
| 103 | + |
| 104 | +The callback function is provided the following arguments: |
| 105 | + |
| 106 | +- **arg0**: current array element for the first broadcasted array. |
| 107 | +- **arg1**: current array element for the second broadcasted array. |
| 108 | +- **...argN**: current array element for the nth broadcasted array. |
| 109 | +- **index**: current element index. |
| 110 | +- **arrays**: broadcasted arrays. If an argument was broadcasted, the corresponding array is a single-element generic array containing the original element. |
| 111 | + |
| 112 | +The number of `argX` arguments is determined according to the number of provided `args` arguments. If no `args` are provided, the callback is invoked without any arguments. |
| 113 | + |
| 114 | +</section> |
| 115 | + |
| 116 | +<!-- /.usage --> |
| 117 | + |
| 118 | +<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 119 | + |
| 120 | +<section class="notes"> |
| 121 | + |
| 122 | +## Notes |
| 123 | + |
| 124 | +- If the function is provided array-like objects of unequal lengths, the function throws an error. |
| 125 | +- The function supports array-like objects supporting the accessor protocol (e.g., [`Complex128Array`][@stdlib/array/complex128], [`Complex64Array`][@stdlib/array/complex64], etc). |
| 126 | + |
| 127 | +</section> |
| 128 | + |
| 129 | +<!-- /.notes --> |
| 130 | + |
| 131 | +<!-- Package usage examples. --> |
| 132 | + |
| 133 | +<section class="examples"> |
| 134 | + |
| 135 | +## Examples |
| 136 | + |
| 137 | +<!-- eslint no-undef: "error" --> |
| 138 | + |
| 139 | +```javascript |
| 140 | +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); |
| 141 | +var logEachMap = require( '@stdlib/console/log-each-map' ); |
| 142 | + |
| 143 | +function add( x, y ) { |
| 144 | + return x + y; |
| 145 | +} |
| 146 | + |
| 147 | +var x = discreteUniform( 10, -50, 50, { |
| 148 | + 'dtype': 'float64' |
| 149 | +}); |
| 150 | +var y = discreteUniform( 10, -50, 50, { |
| 151 | + 'dtype': 'float64' |
| 152 | +}); |
| 153 | + |
| 154 | +logEachMap( '%d + %d = %d', x, y, add ); |
| 155 | +``` |
| 156 | + |
| 157 | +</section> |
| 158 | + |
| 159 | +<!-- /.examples --> |
| 160 | + |
| 161 | +<!-- 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. --> |
| 162 | + |
| 163 | +<section class="references"> |
| 164 | + |
| 165 | +</section> |
| 166 | + |
| 167 | +<!-- /.references --> |
| 168 | + |
| 169 | +<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> |
| 170 | + |
| 171 | +<section class="related"> |
| 172 | + |
| 173 | +</section> |
| 174 | + |
| 175 | +<!-- /.related --> |
| 176 | + |
| 177 | +<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> |
| 178 | + |
| 179 | +<section class="links"> |
| 180 | + |
| 181 | +[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128 |
| 182 | + |
| 183 | +[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64 |
| 184 | + |
| 185 | +</section> |
| 186 | + |
| 187 | +<!-- /.links --> |
0 commit comments