Skip to content

Commit f48f437

Browse files
committed
feat: add console/logEachMap
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 47119e4 commit f48f437

File tree

11 files changed

+1229
-0
lines changed

11 files changed

+1229
-0
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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 into a format string, apply a callback, 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 into a format string, applies a callback, 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+
- **values**: Current array elements or broadcasted values.
107+
- **index**: Current element index.
108+
- **arrays**: Input arrays or broadcasted arrays.
109+
110+
</section>
111+
112+
<!-- /.usage -->
113+
114+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
115+
116+
<section class="notes">
117+
118+
## Notes
119+
120+
- If the function is provided array-like objects of unequal lengths, the function throws an error.
121+
- The function supports array-like objects supporting the accessor protocol (e.g., [`Complex128Array`][@stdlib/array/complex128], [`Complex64Array`][@stdlib/array/complex64], etc).
122+
123+
</section>
124+
125+
<!-- /.notes -->
126+
127+
<!-- Package usage examples. -->
128+
129+
<section class="examples">
130+
131+
## Examples
132+
133+
<!-- eslint no-undef: "error" -->
134+
135+
```javascript
136+
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
137+
var logEachMap = require( '@stdlib/console/log-each-map' );
138+
139+
function add( x, y ) {
140+
return x + y;
141+
}
142+
143+
var x = discreteUniform( 10, -50, 50, {
144+
'dtype': 'float64'
145+
});
146+
var y = discreteUniform( 10, -50, 50, {
147+
'dtype': 'float64'
148+
});
149+
150+
logEachMap( '%d + %d = %d', x, y, add );
151+
```
152+
153+
</section>
154+
155+
<!-- /.examples -->
156+
157+
<!-- 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. -->
158+
159+
<section class="references">
160+
161+
</section>
162+
163+
<!-- /.references -->
164+
165+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
166+
167+
<section class="related">
168+
169+
</section>
170+
171+
<!-- /.related -->
172+
173+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
174+
175+
<section class="links">
176+
177+
[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128
178+
179+
[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64
180+
181+
</section>
182+
183+
<!-- /.links -->
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var proxyquire = require( 'proxyquire' );
24+
var bench = require( '@stdlib/bench' );
25+
var pkg = require( './../package.json' ).name;
26+
27+
28+
// FUNCTIONS //
29+
30+
function clbk( x ) {
31+
return x;
32+
}
33+
34+
35+
// MAIN //
36+
37+
bench( pkg+'::no_collections', function benchmark( b ) {
38+
var logEachMap;
39+
var i;
40+
41+
logEachMap = proxyquire( './../lib/main.js', {
42+
'@stdlib/console/log': logger
43+
});
44+
45+
b.tic();
46+
for ( i = 0; i < b.iterations; i++ ) {
47+
logEachMap( '%d -> %d', i, clbk );
48+
}
49+
b.toc();
50+
b.pass( 'benchmark finished' );
51+
b.end();
52+
53+
function logger( str ) {
54+
if ( typeof str !== 'string' ) {
55+
b.fail( 'should return a string' );
56+
}
57+
}
58+
});
59+
60+
bench( pkg+'::collections:len=1', function benchmark( b ) {
61+
var logEachMap;
62+
var i;
63+
64+
logEachMap = proxyquire( './../lib/main.js', {
65+
'@stdlib/console/log': logger
66+
});
67+
68+
b.tic();
69+
for ( i = 0; i < b.iterations; i++ ) {
70+
logEachMap( '%d -> %d', [ i ], clbk );
71+
}
72+
b.toc();
73+
b.pass( 'benchmark finished' );
74+
b.end();
75+
76+
function logger( str ) {
77+
if ( typeof str !== 'string' ) {
78+
b.fail( 'should return a string' );
79+
}
80+
}
81+
});
82+
83+
bench( pkg+'::collections:len=2', function benchmark( b ) {
84+
var logEachMap;
85+
var i;
86+
87+
logEachMap = proxyquire( './../lib/main.js', {
88+
'@stdlib/console/log': logger
89+
});
90+
91+
b.tic();
92+
for ( i = 0; i < b.iterations; i++ ) {
93+
logEachMap( '%d + %d = %d', [ i ], [ i + 1 ], sum );
94+
}
95+
b.toc();
96+
b.pass( 'benchmark finished' );
97+
b.end();
98+
99+
function sum( x, y ) {
100+
return x + y;
101+
}
102+
103+
function logger( str ) {
104+
if ( typeof str !== 'string' ) {
105+
b.fail( 'should return a string' );
106+
}
107+
}
108+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2025 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var proxyquire = require( 'proxyquire' );
24+
var bench = require( '@stdlib/bench' );
25+
var zeros = require( '@stdlib/array/zeros' );
26+
var pow = require( '@stdlib/math/base/special/pow' );
27+
var pkg = require( './../package.json' ).name;
28+
29+
30+
// FUNCTIONS //
31+
32+
function clbk( x ) {
33+
return x;
34+
}
35+
36+
/**
37+
* Creates a benchmark function.
38+
*
39+
* @private
40+
* @param {PositiveInteger} len - array length
41+
* @returns {Function} benchmark function
42+
*/
43+
function createBenchmark( len ) {
44+
var x = zeros( len, 'float64' );
45+
return benchmark;
46+
47+
/**
48+
* Benchmark function.
49+
*
50+
* @private
51+
* @param {Benchmark} b - benchmark instance
52+
*/
53+
function benchmark( b ) {
54+
var logEachMap;
55+
var i;
56+
57+
logEachMap = proxyquire( './../lib/main.js', {
58+
'@stdlib/console/log': logger
59+
});
60+
61+
b.tic();
62+
for ( i = 0; i < b.iterations; i++ ) {
63+
logEachMap( '%d -> %d', x, clbk );
64+
}
65+
b.toc();
66+
b.pass( 'benchmark finished' );
67+
b.end();
68+
69+
function logger( str ) {
70+
if ( typeof str !== 'string' ) {
71+
b.fail( 'should return a string' );
72+
}
73+
}
74+
}
75+
}
76+
77+
78+
// MAIN //
79+
80+
/**
81+
* Main execution sequence.
82+
*
83+
* @private
84+
*/
85+
function main() {
86+
var len;
87+
var min;
88+
var max;
89+
var f;
90+
var i;
91+
92+
min = 1; // 10^min
93+
max = 6; // 10^max
94+
95+
for ( i = min; i <= max; i++ ) {
96+
len = pow( 10, i );
97+
f = createBenchmark( len );
98+
bench( pkg+'::collections:len='+len, f );
99+
}
100+
}
101+
102+
main();

0 commit comments

Comments
 (0)