Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
183 changes: 183 additions & 0 deletions lib/node_modules/@stdlib/console/log-each-map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<!--
@license Apache-2.0
Copyright (c) 2025 The Stdlib Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

# logEachMap

> Insert array element values into a format string, apply a callback, and print the result.
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->

<section class="intro">

</section>

<!-- /.intro -->

<!-- Package usage documentation. -->

<section class="usage">

## Usage

```javascript
var logEachMap = require( '@stdlib/console/log-each-map' );
```

#### logEachMap( str\[, ...args], clbk\[, thisArg] )

Inserts array element values into a format string, applies a callback, and prints the result.

```javascript
function add( a, b ) {
return a + b;
}

var x = [ 1, 2, 3 ];
var y = [ 4, 5, 6 ];

logEachMap( '%d + %d = %d', x, y, add );
// e.g., => '1 + 4 = 5\n2 + 5 = 7\n3 + 6 = 9\n'
```

The function accepts the following arguments:

- **str**: Format string.
- **args**: Input arrays _(optional)_.
- **clbk**: Callback function.
- **thisArg**: Callback execution context _(optional)_.

To set the callback execution context, provide a `thisArg`.

<!-- eslint-disable no-invalid-this -->

```javascript
function count( x, y ) {
this.count += 1;
return x + y;
}

var x = [ 1, 2, 3 ];
var y = [ 4, 5, 6 ];

var ctx = {
'count': 0
};

logEachMap( '%d + %d = %d', x, y, count, ctx );
// e.g., => '1 + 4 = 5\n2 + 5 = 7\n3 + 6 = 9\n'

var v = ctx.count;
// returns 3
```

If an interpolated argument is not an array-like object, the argument is broadcasted for each iteration.

```javascript
function multiply( x, y ) {
return x * y;
}

var x = [ 1, 2, 3 ];
var y = 2;

logEachMap( '%d * %d = %d', x, y, multiply );
// e.g., => '1 * 2 = 2\n2 * 2 = 4\n3 * 2 = 6\n'
```

The callback function is provided the following arguments:

- **values**: Current array elements or broadcasted values.
- **index**: Current element index.
- **arrays**: Input arrays or broadcasted arrays.

</section>

<!-- /.usage -->

<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="notes">

## Notes

- If the function is provided array-like objects of unequal lengths, the function throws an error.
- The function supports array-like objects supporting the accessor protocol (e.g., [`Complex128Array`][@stdlib/array/complex128], [`Complex64Array`][@stdlib/array/complex64], etc).

</section>

<!-- /.notes -->

<!-- Package usage examples. -->

<section class="examples">

## Examples

<!-- eslint no-undef: "error" -->

```javascript
var discreteUniform = require( '@stdlib/random/array/discrete-uniform' );
var logEachMap = require( '@stdlib/console/log-each-map' );

function add( x, y ) {
return x + y;
}

var x = discreteUniform( 10, -50, 50, {
'dtype': 'float64'
});
var y = discreteUniform( 10, -50, 50, {
'dtype': 'float64'
});

logEachMap( '%d + %d = %d', x, y, add );
```

</section>

<!-- /.examples -->

<!-- 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. -->

<section class="references">

</section>

<!-- /.references -->

<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

<section class="related">

</section>

<!-- /.related -->

<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->

<section class="links">

[@stdlib/array/complex128]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex128

[@stdlib/array/complex64]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/array/complex64

</section>

<!-- /.links -->
108 changes: 108 additions & 0 deletions lib/node_modules/@stdlib/console/log-each-map/benchmark/benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var proxyquire = require( 'proxyquire' );
var bench = require( '@stdlib/bench' );
var pkg = require( './../package.json' ).name;


// FUNCTIONS //

function clbk( x ) {
return x;
}


// MAIN //

bench( pkg+'::no_collections', function benchmark( b ) {
var logEachMap;
var i;

logEachMap = proxyquire( './../lib/main.js', {
'@stdlib/console/log': logger
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
logEachMap( '%d -> %d', i, clbk );
}
b.toc();
b.pass( 'benchmark finished' );
b.end();

function logger( str ) {
if ( typeof str !== 'string' ) {
b.fail( 'should return a string' );
}
}
});

bench( pkg+'::collections:len=1', function benchmark( b ) {
var logEachMap;
var i;

logEachMap = proxyquire( './../lib/main.js', {
'@stdlib/console/log': logger
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
logEachMap( '%d -> %d', [ i ], clbk );
}
b.toc();
b.pass( 'benchmark finished' );
b.end();

function logger( str ) {
if ( typeof str !== 'string' ) {
b.fail( 'should return a string' );
}
}
});

bench( pkg+'::collections:len=2', function benchmark( b ) {
var logEachMap;
var i;

logEachMap = proxyquire( './../lib/main.js', {
'@stdlib/console/log': logger
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
logEachMap( '%d + %d = %d', [ i ], [ i + 1 ], sum );
}
b.toc();
b.pass( 'benchmark finished' );
b.end();

function sum( x, y ) {
return x + y;
}

function logger( str ) {
if ( typeof str !== 'string' ) {
b.fail( 'should return a string' );
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2025 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var proxyquire = require( 'proxyquire' );
var bench = require( '@stdlib/bench' );
var zeros = require( '@stdlib/array/zeros' );
var pow = require( '@stdlib/math/base/special/pow' );
var pkg = require( './../package.json' ).name;


// FUNCTIONS //

function clbk( x ) {
return x;
}

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - array length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var x = zeros( len, 'float64' );
return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var logEachMap;
var i;

logEachMap = proxyquire( './../lib/main.js', {
'@stdlib/console/log': logger
});

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
logEachMap( '%d -> %d', x, clbk );
}
b.toc();
b.pass( 'benchmark finished' );
b.end();

function logger( str ) {
if ( typeof str !== 'string' ) {
b.fail( 'should return a string' );
}
}
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( pkg+'::collections:len='+len, f );
}
}

main();
Loading
Loading