From db36990774f18d11a7b3121fb91fc5eb049ea87e Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Fri, 28 Jul 2023 22:11:03 -0600 Subject: [PATCH 1/4] feat: add `array/base/flatten4d-by` --- .../@stdlib/array/base/flatten4d-by/README.md | 197 ++++++++ .../base/flatten4d-by/benchmark/benchmark.js | 143 ++++++ .../array/base/flatten4d-by/docs/repl.txt | 100 +++++ .../base/flatten4d-by/docs/types/index.d.ts | 195 ++++++++ .../base/flatten4d-by/docs/types/test.ts | 340 ++++++++++++++ .../array/base/flatten4d-by/examples/index.js | 69 +++ .../array/base/flatten4d-by/lib/assign.js | 120 +++++ .../array/base/flatten4d-by/lib/index.js | 82 ++++ .../array/base/flatten4d-by/lib/main.js | 113 +++++ .../array/base/flatten4d-by/package.json | 70 +++ .../base/flatten4d-by/test/test.assign.js | 423 ++++++++++++++++++ .../array/base/flatten4d-by/test/test.js | 40 ++ .../array/base/flatten4d-by/test/test.main.js | 250 +++++++++++ 13 files changed, 2142 insertions(+) create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/README.md create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/benchmark/benchmark.js create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/docs/repl.txt create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/docs/types/index.d.ts create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/docs/types/test.ts create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/examples/index.js create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/lib/assign.js create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/lib/index.js create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/lib/main.js create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/package.json create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.assign.js create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.js create mode 100644 lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.main.js diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/README.md b/lib/node_modules/@stdlib/array/base/flatten4d-by/README.md new file mode 100644 index 00000000000..6aa2e2e7acb --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/README.md @@ -0,0 +1,197 @@ + + + + +# flatten4dBy + +> Flatten a four-dimensional nested array according to a callback function. + +
+ +## Usage + +```javascript +var flatten4dBy = require( '@stdlib/array/base/flatten4d-by' ); +``` + +#### flatten4dBy( x, shape, colexicographic, clbk\[, thisArg] ) + +Flattens a four-dimensional nested array according to a callback function. + +```javascript +function scale( v ) { + return v * 2; +} + +var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + +var out = flatten4dBy( x, [ 2, 1, 1, 2 ], false, scale ); +// returns [ 2, 4, 6, 8 ] +``` + +To flatten in colexicographic order, provide a third argument equal to `true`. + +```javascript +function scale( v ) { + return v * 2; +} + +var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + +var out = flatten4dBy( x, [ 2, 1, 1, 2 ], true, scale ); +// returns [ 2, 6, 4, 8 ] +``` + +To set the callback execution context, provide a `thisArg` argument. + + + +```javascript +function scale( v ) { + this.count += 1; + return v * 2; +} + +var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +var ctx = { + 'count': 0 +}; + +var out = flatten4dBy( x, [ 2, 1, 1, 2 ], false, scale, ctx ); +// returns [ 2, 4, 6, 8 ] + +var count = ctx.count; +// returns 4 +``` + +#### flatten4dBy.assign( x, shape, colexicographic, out, stride, offset, clbk\[, thisArg] ) + +Flattens a four-dimensional nested array according to a callback function and assigns elements to a provided output array. + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); + +function scale( v ) { + return v * 2; +} + +var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +var out = new Float64Array( 4 ); + +var y = flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0 ); +// returns [ 2, 4, 6, 8 ] + +var bool = ( y === out ); +// returns true + +y = flatten4dBy.assign( x, [ 2, 1, 1, 2 ], true, out, 1, 0 ); +// returns [ 2, 6, 4, 8 ] +``` + +
+ + + +
+ +## Notes + +- A callback function is provided the following arguments: + + - **value**: nested array element. + - **indices**: element indices (in lexicographic order). + - **arr**: the input array. + +- Both functions assume that all nested arrays have the same length (i.e., the input array is **not** a ragged array). + +
+ + + +
+ +## Examples + + + +```javascript +var naryFunction = require( '@stdlib/utils/nary-function' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var flatten4dBy = require( '@stdlib/array/base/flatten4d-by' ); + +var fcn = naryFunction( abs, 1 ); + +// Define a 2x2x2x2 array: +var x = [ + [ + [ [ -1, -2 ], [ -3, -4 ] ], + [ [ -5, -6 ], [ -7, -8 ] ] + ], + [ + [ [ -9, -10 ], [ -11, -12 ] ], + [ [ -13, -14 ], [ -15, -16 ] ] + ] +]; + +var out = flatten4dBy( x, [ 0, 0, 0, 0 ], false, fcn ); +// returns [] + +out = flatten4dBy( x, [ 0, 0, 0, 0 ], true, fcn ); +// returns [] + +out = flatten4dBy( x, [ 1, 1, 1, 1 ], false, fcn ); +// returns [ 1 ] + +out = flatten4dBy( x, [ 1, 1, 1, 1 ], true, fcn ); +// returns [ 1 ] + +out = flatten4dBy( x, [ 1, 2, 2, 2 ], false, fcn ); +// returns [ 1, 2, 3, 4, 5, 6, 7, 8 ] + +out = flatten4dBy( x, [ 1, 2, 2, 2 ], true, fcn ); +// returns [ 1, 5, 3, 7, 2, 6, 4, 8 ] + +out = flatten4dBy( x, [ 2, 2, 2, 2 ], false, fcn ); +// returns [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ] + +out = flatten4dBy( x, [ 2, 2, 2, 2 ], true, fcn ); +// returns [ 1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6, 14, 4, 12, 8, 16 ] +``` + +
+ + + + + + + + + + + + + + diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/benchmark/benchmark.js b/lib/node_modules/@stdlib/array/base/flatten4d-by/benchmark/benchmark.js new file mode 100644 index 00000000000..6243415ea3a --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/benchmark/benchmark.js @@ -0,0 +1,143 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 bench = require( '@stdlib/bench' ); +var isArray = require( '@stdlib/assert/is-array' ); +var isFloat64Array = require( '@stdlib/assert/is-float64array' ); +var zeroTo = require( '@stdlib/array/base/zero-to' ); +var filled = require( '@stdlib/array/base/filled' ); +var Float64Array = require( '@stdlib/array/float64' ); +var pkg = require( './../package.json' ).name; +var flatten4dBy = require( './../lib' ); + + +// FUNCTIONS // + +/** +* Scales an input value. +* +* @private +* @param {number} v - input value +* @returns {number} output value +* +* @example +* var v = scale( 2.0 ); +* // returns 4.0 +*/ +function scale( v ) { + return v * 2.0; +} + + +// MAIN // + +bench( pkg+':size=144,lexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = filled( filled( filled( zeroTo( 3 ), 4 ), 3 ), 4 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = flatten4dBy( x, [ 4, 3, 4, 3 ], false, scale ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':size=144,colexicographic', function benchmark( b ) { + var x; + var i; + var v; + + x = filled( filled( filled( zeroTo( 3 ), 4 ), 3 ), 4 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = flatten4dBy( x, [ 4, 3, 4, 3 ], true, scale ); + if ( typeof v !== 'object' ) { + b.fail( 'should return an array' ); + } + } + b.toc(); + if ( !isArray( v ) ) { + b.fail( 'should return an array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':assign:size=144,lexicographic', function benchmark( b ) { + var out; + var x; + var i; + var v; + + out = new Float64Array( 144 ); + x = filled( filled( filled( zeroTo( 3 ), 4 ), 3 ), 4 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = flatten4dBy.assign( x, [ 4, 3, 4, 3 ], false, out, 1, 0, scale ); + if ( typeof v !== 'object' ) { + b.fail( 'should return a Float64Array' ); + } + } + b.toc(); + if ( !isFloat64Array( v ) ) { + b.fail( 'should return a Float64Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); + +bench( pkg+':assign:size=144,colexicographic', function benchmark( b ) { + var out; + var x; + var i; + var v; + + out = new Float64Array( 144 ); + x = filled( filled( filled( zeroTo( 3 ), 4 ), 3 ), 4 ); + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + v = flatten4dBy.assign( x, [ 4, 3, 4, 3 ], true, out, 1, 0, scale ); + if ( typeof v !== 'object' ) { + b.fail( 'should return a Float64Array' ); + } + } + b.toc(); + if ( !isFloat64Array( v ) ) { + b.fail( 'should return a Float64Array' ); + } + b.pass( 'benchmark finished' ); + b.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/repl.txt b/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/repl.txt new file mode 100644 index 00000000000..18ce60bf3d2 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/repl.txt @@ -0,0 +1,100 @@ + +{{alias}}( x, shape, colex, clbk[, thisArg] ) + Flattens a four-dimensional nested array according to a callback function. + + The function assumes that all nested arrays have the same length (i.e., the + input array is *not* a ragged array). + + Parameters + ---------- + x: ArrayLikeObject + Input array. + + shape: Array + Array shape. + + colex: boolean + Specifies whether to flatten array values in colexicographic order. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: Array + Flattened array. + + Examples + -------- + > function fcn( v ) { return v * 2; }; + > var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + > var out = {{alias}}( x, [ 2, 1, 1, 2 ], false, fcn ) + [ 2, 4, 6, 8 ] + > out = {{alias}}( x, [ 2, 1, 1, 2 ], true, fcn ) + [ 2, 6, 4, 8 ] + + +{{alias}}.assign( x, shape, colex, out, stride, offset, clbk[, thisArg] ) + Flattens a four-dimensional nested array according to a callback function + and assigns elements to a provided output array. + + The function assumes that all nested arrays have the same length (i.e., the + input array is *not* a ragged array). + + The callback function is provided the following arguments: + + - value: nested array element. + - indices: element indices (in lexicographic order). + - arr: the input array. + + Parameters + ---------- + x: Array + Input array. + + shape: Array + Array shape. + + colex: boolean + Specifies whether to flatten array values in colexicographic order. + + out: Collection + Output array. + + stride: integer + Output array stride. + + offset: integer + Output array index offset. + + clbk: Function + Callback function. + + thisArg: any (optional) + Callback execution context. + + Returns + ------- + out: Array + Output array. + + Examples + -------- + > function fcn( v ) { return v * 2; }; + > var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + > var out = [ 0, 0, 0, 0 ]; + > var v = {{alias}}.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, fcn ) + [ 2, 4, 6, 8 ] + > var bool = ( v === out ) + true + > out = [ 0, 0, 0, 0 ]; + > {{alias}}.assign( x, [ 2, 1, 1, 2 ], true, out, 1, 0, fcn ); + > out + [ 2, 6, 4, 8 ] + + See Also + -------- + diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/types/index.d.ts b/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/types/index.d.ts new file mode 100644 index 00000000000..2950453f392 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/types/index.d.ts @@ -0,0 +1,195 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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. +*/ + +// TypeScript Version: 4.1 + +/// + +import { Collection } from '@stdlib/types/object'; + +/** +* Four-dimensional nested array. +*/ +type Array4D = Array>>>; + +/** +* Nullary callback function. +* +* @returns result +*/ +type Nullary = () => U; + +/** +* Unary callback function. +* +* @param value - array element +* @returns result +*/ +type Unary = ( value: T ) => U; + +/** +* Binary callback function. +* +* @param value - array element +* @param indices - element indices +* @returns result +*/ +type Binary = ( value: T, indices: Array ) => U; + +/** +* Ternary callback function. +* +* @param value - array element +* @param indices - element indices +* @param arr - input array +* @returns result +*/ +type Ternary = ( value: T, indices: Array, arr: Array4D ) => U; + +/** +* Callback function. +* +* @param value - array element +* @param indices - element indices +* @param arr - input array +* @returns result +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Interface describing `flatten4dBy`. +*/ +interface Flatten4dBy { + /** + * Flattens a four-dimensional nested array according to a callback function. + * + * ## Notes + * + * - The function assumes that all nested arrays have the same length (i.e., the input array is **not** a ragged array). + * + * @param x - input array + * @param shape - array shape + * @param colexicographic - specifies whether to flatten array values in colexicographic order + * @param clbk - callback function + * @param thisArg - callback execution context + * @returns flattened array + * + * @example + * function scale( v ) { + * return v * 2; + * } + * + * var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + * + * var out = flatten4dBy( x, [ 2, 1, 1, 2 ], false, scale ); + * // returns [ 2, 4, 6, 8 ] + * + * @example + * function scale( v ) { + * return v * 2; + * } + * + * var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + * + * var out = flatten4dBy( x, [ 2, 1, 1, 2 ], true, scale ); + * // returns [ 2, 6, 4, 8 ] + */ + ( x: Array4D, shape: Collection, colexicographic: boolean, clbk: Callback, thisArg?: ThisParameterType> ): Array; + + /** + * Flattens a four-dimensional nested array according to a callback function and assigns elements to a provided output array. + * + * ## Notes + * + * - The function assumes that all nested arrays have the same length (i.e., the input array is **not** a ragged array). + * + * @param x - input array + * @param shape - array shape + * @param colexicographic - specifies whether to flatten array values in colexicographic order + * @param out - output array + * @param stride - output array stride + * @param offset - output array index offset + * @param clbk - callback function + * @param thisArg - callback execution context + * @returns output array + * + * @example + * var Float64Array = require( `@stdlib/array/float64` ); + * + * function scale( v ) { + * return v * 2; + * } + * + * var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + * + * var out = flatten4dBy.assign( x, [ 2, 2 ], false, new Float64Array( 4 ), 1, 0, scale ); + * // returns [ 2, 4, 6, 8 ] + * + * @example + * var Float64Array = require( `@stdlib/array/float64` ); + * + * function scale( v ) { + * return v * 2; + * } + * + * var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + * + * var out = flatten4dBy.assign( x, [ 2, 2 ], true, new Float64Array( 4 ), 1, 0, scale ); + * // returns [ 2, 6, 4, 8 ] + */ + assign( x: Array4D, shape: Collection, colexicographic: boolean, out: Collection, stride: number, offset: number, clbk: Callback, thisArg?: ThisParameterType> ): Collection; +} + +/** +* Flattens a four-dimensional nested array according to a callback function. +* +* ## Notes +* +* - The function assumes that all nested arrays have the same length (i.e., the input array is **not** a ragged array). +* +* @param x - input array +* @param shape - array shape +* @param colexicographic - specifies whether to flatten array values in colexicographic order +* @returns flattened array +* +* @example +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = flatten4dBy( x, [ 2, 1, 1, 2 ], false, scale ); +* // returns [ 2, 4, 6, 8 ] +* +* @example +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = flatten4dBy( x, [ 2, 1, 1, 2 ], true, scale ); +* // returns [ 2, 6, 4, 8 ] +*/ +declare var flatten4dBy: Flatten4dBy; + + +// EXPORTS // + +export = flatten4dBy; diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/types/test.ts b/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/types/test.ts new file mode 100644 index 00000000000..81a42dc7a5e --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/docs/types/test.ts @@ -0,0 +1,340 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2023 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. +*/ + +import flatten4dBy = require( './index' ); + +/** +* Identity function. +* +* @param v - input value +* @returns input value +*/ +function identity( value: T ): T { + return value; +} + + +// TESTS // + +// The function returns an array... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + + flatten4dBy( x, [ 2, 1, 1, 2 ], false, identity ); // $ExpectType number[] + flatten4dBy( x, [ 2, 1, 1, 2 ], true, identity ); // $ExpectType number[] + + flatten4dBy( x, [ 2, 1, 1, 2 ], false, identity, {} ); // $ExpectType number[] + flatten4dBy( x, [ 2, 1, 1, 2 ], true, identity, {} ); // $ExpectType number[] + + flatten4dBy( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], false, identity ); // $ExpectType string[] + flatten4dBy( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], true, identity ); // $ExpectType string[] + + flatten4dBy( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], false, identity, {} ); // $ExpectType string[] + flatten4dBy( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], true, identity, {} ); // $ExpectType string[] +} + +// The compiler throws an error if the function is provided a first argument which is not an array-like object... +{ + flatten4dBy( 1, [ 2, 1, 1, 2 ], false, identity ); // $ExpectError + flatten4dBy( true, [ 2, 1, 1, 2 ], false, identity ); // $ExpectError + flatten4dBy( false, [ 2, 1, 1, 2 ], false, identity ); // $ExpectError + flatten4dBy( null, [ 2, 1, 1, 2 ], false, identity ); // $ExpectError + flatten4dBy( void 0, [ 2, 1, 1, 2 ], false, identity ); // $ExpectError + flatten4dBy( {}, [ 2, 1, 1, 2 ], false, identity ); // $ExpectError + + flatten4dBy( 1, [ 2, 1, 1, 2 ], false, identity, {} ); // $ExpectError + flatten4dBy( true, [ 2, 1, 1, 2 ], false, identity, {} ); // $ExpectError + flatten4dBy( false, [ 2, 1, 1, 2 ], false, identity, {} ); // $ExpectError + flatten4dBy( null, [ 2, 1, 1, 2 ], false, identity, {} ); // $ExpectError + flatten4dBy( void 0, [ 2, 1, 1, 2 ], false, identity, {} ); // $ExpectError + flatten4dBy( {}, [ 2, 1, 1, 2 ], false, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a second argument which is not an array-like object containing numbers... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + + flatten4dBy( x, '', false, identity ); // $ExpectError + flatten4dBy( x, 1, false, identity ); // $ExpectError + flatten4dBy( x, true, false, identity ); // $ExpectError + flatten4dBy( x, false, false, identity ); // $ExpectError + flatten4dBy( x, null, false, identity ); // $ExpectError + flatten4dBy( x, void 0, false, identity ); // $ExpectError + flatten4dBy( x, {}, false, identity ); // $ExpectError + flatten4dBy( x, [ 1, '2', 3 ], false, identity ); // $ExpectError + flatten4dBy( x, ( x: number ): number => x, false, identity ); // $ExpectError + + flatten4dBy( x, '', false, identity, {} ); // $ExpectError + flatten4dBy( x, 1, false, identity, {} ); // $ExpectError + flatten4dBy( x, true, false, identity, {} ); // $ExpectError + flatten4dBy( x, false, false, identity, {} ); // $ExpectError + flatten4dBy( x, null, false, identity, {} ); // $ExpectError + flatten4dBy( x, void 0, false, identity, {} ); // $ExpectError + flatten4dBy( x, {}, false, identity, {} ); // $ExpectError + flatten4dBy( x, [ 1, '2', 3 ], false, identity, {} ); // $ExpectError + flatten4dBy( x, ( x: number ): number => x, false, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a third argument which is not a boolean... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + + flatten4dBy( x, [ 2, 1, 1, 2 ], '', identity ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], 1, identity ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], null, identity ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], void 0, identity ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], {}, identity ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], [], identity ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], ( x: number ): number => x, identity ); // $ExpectError + + flatten4dBy( x, [ 2, 1, 1, 2 ], '', identity, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], 1, identity, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], null, identity, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], void 0, identity, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], {}, identity, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], [], identity, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], ( x: number ): number => x, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a fourth argument which is not a function... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + + flatten4dBy( x, [ 2, 1, 1, 2 ], false, '' ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, 1 ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, true ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, false ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, null ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, void 0 ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, [] ); // $ExpectError + + flatten4dBy( x, [ 2, 1, 1, 2 ], false, '', {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, 1, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, true, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, false, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, null, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, void 0, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, {}, {} ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, [], {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + + flatten4dBy(); // $ExpectError + flatten4dBy( x ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ] ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false ); // $ExpectError + flatten4dBy( x, [ 2, 1, 1, 2 ], false, identity, {}, {} ); // $ExpectError +} + +// Attached to the main export is an `assign` method which returns a collection... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + + const out1 = [ 0, 0, 0, 0 ]; + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out1, 1, 0, identity ); // $ExpectType Collection + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], true, out1, 1, 0, identity ); // $ExpectType Collection + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out1, 1, 0, identity, {} ); // $ExpectType Collection + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], true, out1, 1, 0, identity, {} ); // $ExpectType Collection + + const out2 = [ '', '', '', '' ]; + + flatten4dBy.assign( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], false, out2, 1, 0, identity ); // $ExpectType Collection + flatten4dBy.assign( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], true, out2, 1, 0, identity ); // $ExpectType Collection + + flatten4dBy.assign( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], false, out2, 1, 0, identity, {} ); // $ExpectType Collection + flatten4dBy.assign( [ [ [ [ '1', '2' ] ] ], [ [ [ '3', '4' ] ] ] ], [ 2, 1, 1, 2 ], true, out2, 1, 0, identity, {} ); // $ExpectType Collection +} + +// The compiler throws an error if the `assign` method is provided a first argument which is not an array-like object... +{ + const out = [ 0, 0, 0, 0 ]; + + flatten4dBy.assign( 1, [ 2, 1, 1, 2 ], false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( true, [ 2, 1, 1, 2 ], false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( false, [ 2, 1, 1, 2 ], false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( null, [ 2, 1, 1, 2 ], false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( void 0, [ 2, 1, 1, 2 ], false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( {}, [ 2, 1, 1, 2 ], false, out, 1, 0, identity ); // $ExpectError + + flatten4dBy.assign( 1, [ 2, 1, 1, 2 ], false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( true, [ 2, 1, 1, 2 ], false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( false, [ 2, 1, 1, 2 ], false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( null, [ 2, 1, 1, 2 ], false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( void 0, [ 2, 1, 1, 2 ], false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( {}, [ 2, 1, 1, 2 ], false, out, 1, 0, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a second argument which is not an array-like object containing numbers... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + const out = [ 0, 0, 0, 0 ]; + + flatten4dBy.assign( x, '', false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, 1, false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, true, false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, false, false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, null, false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, void 0, false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, {}, false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 1, '2', 3 ], false, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, ( x: number ): number => x, false, out, 1, 0, identity ); // $ExpectError + + flatten4dBy.assign( x, '', false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, 1, false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, true, false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, false, false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, null, false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, void 0, false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, {}, false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 1, '2', 3 ], false, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, ( x: number ): number => x, false, out, 1, 0, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a third argument which is not a boolean... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + const out = [ 0, 0, 0, 0 ]; + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], '', out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], 1, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], null, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], void 0, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], {}, out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], [], out, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], ( x: number ): number => x, out, 1, 0, identity ); // $ExpectError + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], '', out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], 1, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], null, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], void 0, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], {}, out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], [], out, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], ( x: number ): number => x, out, 1, 0, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fourth argument which is not an array-like object... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, 1, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, true, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, false, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, null, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, void 0, 1, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, {}, 1, 0, identity ); // $ExpectError + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, 1, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, true, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, false, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, null, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, void 0, 1, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, {}, 1, 0, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a fifth argument which is not a number... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + const out = [ 0, 0, 0, 0 ]; + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, '1', 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, true, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, false, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, null, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, void 0, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, {}, 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, [], 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, ( x: number ): number => x, 0, identity ); // $ExpectError + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, '1', 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, true, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, false, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, null, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, void 0, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, {}, 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, [], 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, ( x: number ): number => x, 0, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a sixth argument which is not a number... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + const out = [ 0, 0, 0, 0 ]; + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, '1', identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, true, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, false, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, null, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, void 0, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, {}, identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, [], identity ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, ( x: number ): number => x, identity ); // $ExpectError + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, '1', identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, true, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, false, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, null, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, void 0, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, {}, identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, [], identity, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, ( x: number ): number => x, identity, {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided a seventh argument which is not a function... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + const out = [ 0, 0, 0, 0 ]; + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, '1' ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, 1 ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, true ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, false ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, null ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, void 0 ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, [] ); // $ExpectError + + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, '1', {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, 1, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, true, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, false, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, null, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, void 0, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, {}, {} ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, [], {} ); // $ExpectError +} + +// The compiler throws an error if the `assign` method is provided an unsupported number of arguments... +{ + const x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; + const out = [ 0, 0, 0, 0 ]; + + flatten4dBy.assign(); // $ExpectError + flatten4dBy.assign( x ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ] ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1 ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0 ); // $ExpectError + flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, identity, {}, {} ); // $ExpectError +} diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/examples/index.js b/lib/node_modules/@stdlib/array/base/flatten4d-by/examples/index.js new file mode 100644 index 00000000000..eb5f6a3417b --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/examples/index.js @@ -0,0 +1,69 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +var naryFunction = require( '@stdlib/utils/nary-function' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var flatten4dBy = require( './../lib' ); + +var fcn = naryFunction( abs, 1 ); + +// Define a 2x2x2x2 array: +var x = [ + [ + [ [ -1, -2 ], [ -3, -4 ] ], + [ [ -5, -6 ], [ -7, -8 ] ] + ], + [ + [ [ -9, -10 ], [ -11, -12 ] ], + [ [ -13, -14 ], [ -15, -16 ] ] + ] +]; + +var out = flatten4dBy( x, [ 0, 0, 0, 0 ], false, fcn ); +console.log( out ); +// => [] + +out = flatten4dBy( x, [ 0, 0, 0, 0 ], true, fcn ); +console.log( out ); +// => [] + +out = flatten4dBy( x, [ 1, 1, 1, 1 ], false, fcn ); +console.log( out ); +// => [ 1 ] + +out = flatten4dBy( x, [ 1, 1, 1, 1 ], true, fcn ); +console.log( out ); +// => [ 1 ] + +out = flatten4dBy( x, [ 1, 2, 2, 2 ], false, fcn ); +console.log( out ); +// => [ 1, 2, 3, 4, 5, 6, 7, 8 ] + +out = flatten4dBy( x, [ 1, 2, 2, 2 ], true, fcn ); +console.log( out ); +// => [ 1, 5, 3, 7, 2, 6, 4, 8 ] + +out = flatten4dBy( x, [ 2, 2, 2, 2 ], false, fcn ); +console.log( out ); +// => [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ] + +out = flatten4dBy( x, [ 2, 2, 2, 2 ], true, fcn ); +console.log( out ); +// => [ 1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6, 14, 4, 12, 8, 16 ] diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/assign.js b/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/assign.js new file mode 100644 index 00000000000..2e546441540 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/assign.js @@ -0,0 +1,120 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MAIN // + +/** +* Flattens a four-dimensional nested array according to a callback function and assigns elements to a provided output array. +* +* ## Notes +* +* - The function assumes that all nested arrays have the same length (i.e., the input array is **not** a ragged array). +* +* @param {Array>} x - input nested array +* @param {NonNegativeIntegerArray} shape - array shape +* @param {boolean} colexicographic - specifies whether to flatten array values in colexicographic order +* @param {Collection} out - output array +* @param {integer} stride - output array stride +* @param {NonNegativeInteger} offset - output array index offset +* @param {Function} clbk - callback function +* @param {*} [thisArg] - callback execution context +* @returns {Collection} output array +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = flatten4dBy( x, [ 2, 1, 1, 2 ], false, new Float64Array( 4 ), 1, 0, scale ); +* // returns [ 2, 4, 6, 8 ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = flatten4dBy( x, [ 2, 1, 1, 2 ], true, new Float64Array( 4 ), 1, 0, scale ); +* // returns [ 2, 6, 4, 8 ] +*/ +function flatten4dBy( x, shape, colexicographic, out, stride, offset, clbk, thisArg ) { + var S0; + var S1; + var S2; + var S3; + var i0; + var i1; + var i2; + var i3; + var a0; + var a1; + var a2; + var io; + + // Extract loop variables: + S0 = shape[ 3 ]; // for nested arrays, the last dimensions have the fastest changing indices + S1 = shape[ 2 ]; + S2 = shape[ 1 ]; + S3 = shape[ 0 ]; + + // Iterate over the array dimensions... + io = offset; + if ( colexicographic ) { + for ( i0 = 0; i0 < S0; i0++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + out[ io ] = clbk.call( thisArg, x[ i3 ][ i2 ][ i1 ][ i0 ], [ i3, i2, i1, i0 ], x ); // equivalent to storing in column-major (Fortran-style) order + io += stride; + } + } + } + } + return out; + } + for ( i3 = 0; i3 < S3; i3++ ) { + a2 = x[ i3 ]; + for ( i2 = 0; i2 < S2; i2++ ) { + a1 = a2[ i2 ]; + for ( i1 = 0; i1 < S1; i1++ ) { + a0 = a1[ i1 ]; + for ( i0 = 0; i0 < S0; i0++ ) { + out[ io ] = clbk.call( thisArg, a0[ i0 ], [ i3, i2, i1, i0 ], x ); // equivalent to storing in row-major (C-style) order + io += stride; + } + } + } + } + return out; +} + + +// EXPORTS // + +module.exports = flatten4dBy; diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/index.js b/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/index.js new file mode 100644 index 00000000000..1a8648cdec3 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/index.js @@ -0,0 +1,82 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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'; + +/** +* Flatten a four-dimensional nested array according to a callback function. +* +* @module @stdlib/array/base/flatten4d-by +* +* @example +* var flatten4dBy = require( '@stdlib/array/base/flatten4d-by' ); +* +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = flatten4dBy( x, [ 2, 1, 1, 2 ], false, scale ); +* // returns [ 2, 4, 6, 8 ] +* +* @example +* var flatten4dBy = require( '@stdlib/array/base/flatten4d-by' ); +* +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = flatten4dBy( x, [ 2, 1, 1, 2 ], true, scale ); +* // returns [ 2, 6, 4, 8 ] +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var flatten4dBy = require( '@stdlib/array/base/flatten4d-by' ); +* +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = new Float64Array( 4 ); +* var y = flatten4dBy.assign( x, [ 2, 1, 1, 2 ], true, out, 1, 0, scale ); +* // returns [ 2, 6, 4, 8 ] +* +* var bool = ( y === out ); +* // returns true +*/ + +// MODULES // + +var setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); +var main = require( './main.js' ); +var assign = require( './assign.js' ); + + +// MAIN // + +setReadOnly( main, 'assign', assign ); + + +// EXPORTS // + +module.exports = main; diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/main.js b/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/main.js new file mode 100644 index 00000000000..534bfa127af --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/lib/main.js @@ -0,0 +1,113 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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. +*/ + +/* eslint-disable max-len */ + +'use strict'; + +// MAIN // + +/** +* Flattens a four-dimensional nested array according to a callback function. +* +* ## Notes +* +* - The function assumes that all nested arrays have the same length (i.e., the input array is **not** a ragged array). +* +* @param {Array>} x - input nested array +* @param {NonNegativeIntegerArray} shape - array shape +* @param {boolean} colexicographic - specifies whether to flatten array values in colexicographic order +* @param {Function} clbk - callback function +* @param {*} [thisArg] - callback execution context +* @returns {Array} flattened array +* +* @example +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = flatten4dBy( x, [ 2, 1, 1, 2 ], false, scale ); +* // returns [ 2, 4, 6, 8 ] +* +* @example +* function scale( v ) { +* return v * 2; +* } +* +* var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; +* +* var out = flatten4dBy( x, [ 2, 1, 1, 2 ], true, scale ); +* // returns [ 2, 6, 4, 8 ] +*/ +function flatten4dBy( x, shape, colexicographic, clbk, thisArg ) { + var out; + var S0; + var S1; + var S2; + var S3; + var i0; + var i1; + var i2; + var i3; + var a0; + var a1; + var a2; + + // Extract loop variables: + S0 = shape[ 3 ]; // for nested arrays, the last dimensions have the fastest changing indices + S1 = shape[ 2 ]; + S2 = shape[ 1 ]; + S3 = shape[ 0 ]; + + // Initialize an output array: + out = []; + + // Iterate over the array dimensions... + if ( colexicographic ) { + for ( i0 = 0; i0 < S0; i0++ ) { + for ( i1 = 0; i1 < S1; i1++ ) { + for ( i2 = 0; i2 < S2; i2++ ) { + for ( i3 = 0; i3 < S3; i3++ ) { + out.push( clbk.call( thisArg, x[ i3 ][ i2 ][ i1 ][ i0 ], [ i3, i2, i1, i0 ], x ) ); // equivalent to storing in column-major (Fortran-style) order + } + } + } + } + return out; + } + for ( i3 = 0; i3 < S3; i3++ ) { + a2 = x[ i3 ]; + for ( i2 = 0; i2 < S2; i2++ ) { + a1 = a2[ i2 ]; + for ( i1 = 0; i1 < S1; i1++ ) { + a0 = a1[ i1 ]; + for ( i0 = 0; i0 < S0; i0++ ) { + out.push( clbk.call( thisArg, a0[ i0 ], [ i3, i2, i1, i0 ], x ) ); // equivalent to storing in row-major (C-style) order + } + } + } + } + return out; +} + + +// EXPORTS // + +module.exports = flatten4dBy; diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/package.json b/lib/node_modules/@stdlib/array/base/flatten4d-by/package.json new file mode 100644 index 00000000000..fc4282ccca8 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/package.json @@ -0,0 +1,70 @@ +{ + "name": "@stdlib/array/base/flatten4d-by", + "version": "0.0.0", + "description": "Flatten a four-dimensional nested array according to a callback function.", + "license": "Apache-2.0", + "author": { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + }, + "contributors": [ + { + "name": "The Stdlib Authors", + "url": "https://github.com/stdlib-js/stdlib/graphs/contributors" + } + ], + "main": "./lib", + "directories": { + "benchmark": "./benchmark", + "doc": "./docs", + "example": "./examples", + "lib": "./lib", + "test": "./test" + }, + "types": "./docs/types", + "scripts": {}, + "homepage": "https://github.com/stdlib-js/stdlib", + "repository": { + "type": "git", + "url": "git://github.com/stdlib-js/stdlib.git" + }, + "bugs": { + "url": "https://github.com/stdlib-js/stdlib/issues" + }, + "dependencies": {}, + "devDependencies": {}, + "engines": { + "node": ">=0.10.0", + "npm": ">2.7.0" + }, + "os": [ + "aix", + "darwin", + "freebsd", + "linux", + "macos", + "openbsd", + "sunos", + "win32", + "windows" + ], + "keywords": [ + "stdlib", + "utilities", + "utils", + "generic", + "array", + "flatten", + "flat", + "4d", + "tensor", + "strided", + "ndarray", + "multidimensional", + "foreach", + "map", + "transform", + "callback", + "accessor" + ] +} diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.assign.js b/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.assign.js new file mode 100644 index 00000000000..64adc57d3c7 --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.assign.js @@ -0,0 +1,423 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 tape = require( 'tape' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var Float64Array = require( '@stdlib/array/float64' ); +var flatten4dBy = require( './../lib/assign.js' ); + + +// VARIABLES // + +var clbk = naryFunction( abs, 1 ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof flatten4dBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function flattens a nested array (lexicographic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ], + [ + [ -5, -6 ], + [ -7, -8 ] + ] + ], + [ + [ + [ -9, -10 ], + [ -11, -12 ] + ], + [ + [ -13, -14 ], + [ -15, -16 ] + ] + ] + ]; + + expected = new Float64Array( [] ); + out = new Float64Array( 0 ); + actual = flatten4dBy( x, [ 0, 0, 0, 0 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [] ); + out = new Float64Array( 0 ); + actual = flatten4dBy( x, [ 0, 0, 1, 0 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [] ); + out = new Float64Array( 0 ); + actual = flatten4dBy( x, [ 1, 0, 0, 1 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1 ] ); + out = new Float64Array( 1 ); + actual = flatten4dBy( x, [ 1, 1, 1, 1 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 2 ] ); + out = new Float64Array( 2 ); + actual = flatten4dBy( x, [ 1, 1, 1, 2 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 9 ] ); + out = new Float64Array( 2 ); + actual = flatten4dBy( x, [ 2, 1, 1, 1 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 2, 5, 6 ] ); + out = new Float64Array( 4 ); + actual = flatten4dBy( x, [ 1, 2, 1, 2 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 2, 3, 4, 5, 6, 7, 8 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 1, 2, 2, 2 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 5, 9, 13 ] ); + out = new Float64Array( 4 ); + actual = flatten4dBy( x, [ 2, 2, 1, 1 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 2, 5, 6, 9, 10, 13, 14 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 2, 2, 1, 2 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ] ); // eslint-disable-line max-len + out = new Float64Array( 16 ); + actual = flatten4dBy( x, [ 2, 2, 2, 2 ], false, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function flattens a nested array (colexicographic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ], + [ + [ -5, -6 ], + [ -7, -8 ] + ] + ], + [ + [ + [ -9, -10 ], + [ -11, -12 ] + ], + [ + [ -13, -14 ], + [ -15, -16 ] + ] + ] + ]; + + expected = new Float64Array( [] ); + out = new Float64Array( 0 ); + actual = flatten4dBy( x, [ 0, 0, 0, 0 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [] ); + out = new Float64Array( 0 ); + actual = flatten4dBy( x, [ 0, 0, 1, 0 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [] ); + out = new Float64Array( 0 ); + actual = flatten4dBy( x, [ 1, 0, 0, 1 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1 ] ); + out = new Float64Array( 1 ); + actual = flatten4dBy( x, [ 1, 1, 1, 1 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 2 ] ); + out = new Float64Array( 2 ); + actual = flatten4dBy( x, [ 1, 1, 1, 2 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 9 ] ); + out = new Float64Array( 2 ); + actual = flatten4dBy( x, [ 2, 1, 1, 1 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 5, 2, 6 ] ); + out = new Float64Array( 4 ); + actual = flatten4dBy( x, [ 1, 2, 1, 2 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 5, 3, 7, 2, 6, 4, 8 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 1, 2, 2, 2 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 9, 5, 13 ] ); + out = new Float64Array( 4 ); + actual = flatten4dBy( x, [ 2, 2, 1, 1 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 9, 5, 13, 2, 10, 6, 14 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 2, 2, 1, 2 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = new Float64Array( [ 1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6, 14, 4, 12, 8, 16 ] ); // eslint-disable-line max-len + out = new Float64Array( 16 ); + actual = flatten4dBy( x, [ 2, 2, 2, 2 ], true, out, 1, 0, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a stride (lexicographic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + expected = new Float64Array( [ 1, 0, 2, 0, 3, 0, 4, 0 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 1, 1, 2, 2 ], false, out, 2, 0, clbk ); + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing a stride (colexicographic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + expected = new Float64Array( [ 1, 0, 3, 0, 2, 0, 4, 0 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 1, 1, 2, 2 ], true, out, 2, 0, clbk ); + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an offset (lexicographic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + expected = new Float64Array( [ 0, 0, 1, 2, 3, 4, 0, 0 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 1, 1, 2, 2 ], false, out, 1, 2, clbk ); + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports providing an offset (colexicographic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + expected = new Float64Array( [ 0, 0, 1, 3, 2, 4, 0, 0 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 1, 1, 2, 2 ], true, out, 1, 2, clbk ); + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports complex access patterns (lexicographic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + expected = new Float64Array( [ 0, 4, 0, 3, 0, 2, 0, 1 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 1, 1, 2, 2 ], false, out, -2, 7, clbk ); + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports complex access patterns (colexicographic)', function test( t ) { + var expected; + var actual; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + expected = new Float64Array( [ 0, 4, 0, 2, 0, 3, 0, 1 ] ); + out = new Float64Array( 8 ); + actual = flatten4dBy( x, [ 1, 1, 2, 2 ], true, out, -2, 7, clbk ); + t.strictEqual( actual, out, 'returns expected value' ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports setting the callback execution context (lexicographic)', function test( t ) { + var ctx; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + ctx = { + 'count': 0 + }; + + out = new Float64Array( 4 ); + flatten4dBy( x, [ 1, 1, 2, 2 ], false, out, 1, 0, fcn, ctx ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn( value, indices, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + t.strictEqual( indices.length, 4, 'returns expected value' ); + t.strictEqual( value, arr[ indices[0] ][ indices[1] ][ indices[2] ][ indices[3] ], 'returns expected value' ); + t.strictEqual( arr, x, 'returns expected value' ); + } +}); + +tape( 'the function supports setting the callback execution context (colexicographic)', function test( t ) { + var ctx; + var out; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + ctx = { + 'count': 0 + }; + + out = new Float64Array( 4 ); + flatten4dBy( x, [ 1, 1, 2, 2 ], true, out, 1, 0, fcn, ctx ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn( value, indices, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + t.strictEqual( indices.length, 4, 'returns expected value' ); + t.strictEqual( value, arr[ indices[0] ][ indices[1] ][ indices[2] ][ indices[3] ], 'returns expected value' ); + t.strictEqual( arr, x, 'returns expected value' ); + } +}); diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.js b/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.js new file mode 100644 index 00000000000..0f7ef980fef --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.js @@ -0,0 +1,40 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 tape = require( 'tape' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var flatten4dBy = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof flatten4dBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'attached to the main export is an `assign` method', function test( t ) { + t.strictEqual( hasOwnProp( flatten4dBy, 'assign' ), true, 'has property' ); + t.strictEqual( typeof flatten4dBy.assign, 'function', 'has method' ); + t.end(); +}); diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.main.js b/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.main.js new file mode 100644 index 00000000000..9172f224d9e --- /dev/null +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/test/test.main.js @@ -0,0 +1,250 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2023 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 tape = require( 'tape' ); +var naryFunction = require( '@stdlib/utils/nary-function' ); +var abs = require( '@stdlib/math/base/special/abs' ); +var flatten4dBy = require( './../lib/main.js' ); + + +// VARIABLES // + +var clbk = naryFunction( abs, 1 ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof flatten4dBy, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function flattens a nested array (lexicographic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ], + [ + [ -5, -6 ], + [ -7, -8 ] + ] + ], + [ + [ + [ -9, -10 ], + [ -11, -12 ] + ], + [ + [ -13, -14 ], + [ -15, -16 ] + ] + ] + ]; + + expected = []; + actual = flatten4dBy( x, [ 0, 0, 0, 0 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = []; + actual = flatten4dBy( x, [ 0, 0, 1, 0 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = []; + actual = flatten4dBy( x, [ 1, 0, 0, 1 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1 ]; + actual = flatten4dBy( x, [ 1, 1, 1, 1 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2 ]; + actual = flatten4dBy( x, [ 1, 1, 1, 2 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 9 ]; + actual = flatten4dBy( x, [ 2, 1, 1, 1 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2, 5, 6 ]; + actual = flatten4dBy( x, [ 1, 2, 1, 2 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2, 3, 4, 5, 6, 7, 8 ]; + actual = flatten4dBy( x, [ 1, 2, 2, 2 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 5, 9, 13 ]; + actual = flatten4dBy( x, [ 2, 2, 1, 1 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2, 5, 6, 9, 10, 13, 14 ]; + actual = flatten4dBy( x, [ 2, 2, 1, 2 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]; + actual = flatten4dBy( x, [ 2, 2, 2, 2 ], false, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function flattens a nested array (colexicographic)', function test( t ) { + var expected; + var actual; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ], + [ + [ -5, -6 ], + [ -7, -8 ] + ] + ], + [ + [ + [ -9, -10 ], + [ -11, -12 ] + ], + [ + [ -13, -14 ], + [ -15, -16 ] + ] + ] + ]; + + expected = []; + actual = flatten4dBy( x, [ 0, 0, 0, 0 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = []; + actual = flatten4dBy( x, [ 0, 0, 1, 0 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = []; + actual = flatten4dBy( x, [ 1, 0, 0, 1 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1 ]; + actual = flatten4dBy( x, [ 1, 1, 1, 1 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 2 ]; + actual = flatten4dBy( x, [ 1, 1, 1, 2 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 9 ]; + actual = flatten4dBy( x, [ 2, 1, 1, 1 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 5, 2, 6 ]; + actual = flatten4dBy( x, [ 1, 2, 1, 2 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 5, 3, 7, 2, 6, 4, 8 ]; + actual = flatten4dBy( x, [ 1, 2, 2, 2 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 9, 5, 13 ]; + actual = flatten4dBy( x, [ 2, 2, 1, 1 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 9, 5, 13, 2, 10, 6, 14 ]; + actual = flatten4dBy( x, [ 2, 2, 1, 2 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + expected = [ 1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6, 14, 4, 12, 8, 16 ]; + actual = flatten4dBy( x, [ 2, 2, 2, 2 ], true, clbk ); + t.deepEqual( actual, expected, 'returns expected value' ); + + t.end(); +}); + +tape( 'the function supports setting the callback execution context (lexicographic)', function test( t ) { + var ctx; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + ctx = { + 'count': 0 + }; + + flatten4dBy( x, [ 1, 1, 2, 2 ], false, fcn, ctx ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn( value, indices, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + t.strictEqual( indices.length, 4, 'returns expected value' ); + t.strictEqual( value, arr[ indices[0] ][ indices[1] ][ indices[2] ][ indices[3] ], 'returns expected value' ); + t.strictEqual( arr, x, 'returns expected value' ); + } +}); + +tape( 'the function supports setting the callback execution context (colexicographic)', function test( t ) { + var ctx; + var x; + + x = [ + [ + [ + [ -1, -2 ], + [ -3, -4 ] + ] + ] + ]; + + ctx = { + 'count': 0 + }; + + flatten4dBy( x, [ 1, 1, 2, 2 ], true, fcn, ctx ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn( value, indices, arr ) { + this.count += 1; // eslint-disable-line no-invalid-this + t.strictEqual( indices.length, 4, 'returns expected value' ); + t.strictEqual( value, arr[ indices[0] ][ indices[1] ][ indices[2] ][ indices[3] ], 'returns expected value' ); + t.strictEqual( arr, x, 'returns expected value' ); + } +}); From 3dbf75f15770e6f3b3bc1b60d9135f969b19a40f Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Fri, 28 Jul 2023 22:11:39 -0600 Subject: [PATCH 2/4] feat: add `flatten4dBy` to namespace --- lib/node_modules/@stdlib/array/base/lib/index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/node_modules/@stdlib/array/base/lib/index.js b/lib/node_modules/@stdlib/array/base/lib/index.js index e130ae705ba..a44580821d3 100644 --- a/lib/node_modules/@stdlib/array/base/lib/index.js +++ b/lib/node_modules/@stdlib/array/base/lib/index.js @@ -207,6 +207,15 @@ setReadOnly( ns, 'flatten3dBy', require( '@stdlib/array/base/flatten3d-by' ) ); */ setReadOnly( ns, 'flatten4d', require( '@stdlib/array/base/flatten4d' ) ); +/** +* @name flatten4dBy +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/array/base/flatten4d-by} +*/ +setReadOnly( ns, 'flatten4dBy', require( '@stdlib/array/base/flatten4d-by' ) ); + /** * @name flatten5d * @memberof ns From 247f2c8e833e16859a7dcbe9f3bd7c39bdf0c3bb Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Fri, 28 Jul 2023 22:11:58 -0600 Subject: [PATCH 3/4] docs: fix description --- .../@stdlib/array/base/flatten2d-by/docs/repl.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/flatten2d-by/docs/repl.txt b/lib/node_modules/@stdlib/array/base/flatten2d-by/docs/repl.txt index 6c3d75f1be2..a46014b9dc5 100644 --- a/lib/node_modules/@stdlib/array/base/flatten2d-by/docs/repl.txt +++ b/lib/node_modules/@stdlib/array/base/flatten2d-by/docs/repl.txt @@ -44,8 +44,8 @@ {{alias}}.assign( x, shape, colex, out, stride, offset, clbk[, thisArg] ) - Flattens a two-dimensional nested array and assigns elements to a provided - output array. + Flattens a two-dimensional nested array according to a callback function + and assigns elements to a provided output array. The function assumes that all nested arrays have the same length (i.e., the input array is *not* a ragged array). From 1d3e0ad1c4cebb11e1b6caf66d551d9515c821cf Mon Sep 17 00:00:00 2001 From: Athan Reines Date: Fri, 28 Jul 2023 22:15:27 -0600 Subject: [PATCH 4/4] docs: fix example --- lib/node_modules/@stdlib/array/base/flatten4d-by/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/node_modules/@stdlib/array/base/flatten4d-by/README.md b/lib/node_modules/@stdlib/array/base/flatten4d-by/README.md index 6aa2e2e7acb..afb799faf9b 100644 --- a/lib/node_modules/@stdlib/array/base/flatten4d-by/README.md +++ b/lib/node_modules/@stdlib/array/base/flatten4d-by/README.md @@ -96,13 +96,13 @@ function scale( v ) { var x = [ [ [ [ 1, 2 ] ] ], [ [ [ 3, 4 ] ] ] ]; var out = new Float64Array( 4 ); -var y = flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0 ); +var y = flatten4dBy.assign( x, [ 2, 1, 1, 2 ], false, out, 1, 0, scale ); // returns [ 2, 4, 6, 8 ] var bool = ( y === out ); // returns true -y = flatten4dBy.assign( x, [ 2, 1, 1, 2 ], true, out, 1, 0 ); +y = flatten4dBy.assign( x, [ 2, 1, 1, 2 ], true, out, 1, 0, scale ); // returns [ 2, 6, 4, 8 ] ```