diff --git a/CHANGELOG.md b/CHANGELOG.md index dc134854..621d5c9a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ ##### Features +- [`dbfd8f5`](https://github.com/stdlib-js/stdlib/commit/dbfd8f5c81d11be2142ebfc4f2f0bb0316ba7478) - add `filterMap` to namespace - [`cbc4d3f`](https://github.com/stdlib-js/stdlib/commit/cbc4d3f7514b7213cad4f9d2ca5d916e13eeffa5) - add `reject` to namespace - [`831de1b`](https://github.com/stdlib-js/stdlib/commit/831de1b4ba21cda245c073a5412bf1a2e9d7598d) - add `map` and `filter` to namespace - [`8b1548f`](https://github.com/stdlib-js/stdlib/commit/8b1548fb45c1ff131f5edac20cb984344a2d28ec) - update namespace TypeScript declarations [(#3190)](https://github.com/stdlib-js/stdlib/pull/3190) @@ -158,6 +159,28 @@ +
+ +#### [@stdlib/ndarray/filter-map](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/filter-map) + +
+ +
+ +##### Features + +- [`6ff153f`](https://github.com/stdlib-js/stdlib/commit/6ff153f9023cffac527b3243489e6413e989e940) - add `ndarray/filter-map` + +
+ + + +
+ +
+ + +
#### [@stdlib/ndarray/iter](https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/ndarray/iter) @@ -294,6 +317,11 @@ A total of 3 people contributed to this release. Thank you to the following cont
+- [`dbfd8f5`](https://github.com/stdlib-js/stdlib/commit/dbfd8f5c81d11be2142ebfc4f2f0bb0316ba7478) - **feat:** add `filterMap` to namespace _(by Athan Reines)_ +- [`6ff153f`](https://github.com/stdlib-js/stdlib/commit/6ff153f9023cffac527b3243489e6413e989e940) - **feat:** add `ndarray/filter-map` _(by Athan Reines)_ +- [`8d1be60`](https://github.com/stdlib-js/stdlib/commit/8d1be60be03dae4a293d0a2967ab2539d759a498) - **refactor:** remove unnecessary variable _(by Athan Reines)_ +- [`07c9202`](https://github.com/stdlib-js/stdlib/commit/07c92021666d2b439a239397d54a43e5785b3360) - **refactor:** remove unnecessary variable _(by Athan Reines)_ +- [`4cc1f54`](https://github.com/stdlib-js/stdlib/commit/4cc1f54e1c601aefcf00bfa03948f2909eba60be) - **docs:** update example _(by Athan Reines)_ - [`3cd740e`](https://github.com/stdlib-js/stdlib/commit/3cd740ed3e550ee7411139fef930a96216cff5d9) - **docs:** add example _(by Athan Reines)_ - [`855b8c2`](https://github.com/stdlib-js/stdlib/commit/855b8c255abba003e9505aa3a80105a2e2b6b3a7) - **docs:** add example _(by Athan Reines)_ - [`47d03ca`](https://github.com/stdlib-js/stdlib/commit/47d03ca557edea6a39c8fa3cc3262ad85d04cd56) - **docs:** add example _(by Athan Reines)_ diff --git a/filter-map/README.md b/filter-map/README.md new file mode 100644 index 00000000..d103411b --- /dev/null +++ b/filter-map/README.md @@ -0,0 +1,267 @@ + + +# filterMap + +> Filter and map elements in an input [ndarray][@stdlib/ndarray/ctor] to elements in a new output [ndarray][@stdlib/ndarray/ctor] according to a callback function. + +
+ +
+ + + +
+ +## Usage + +```javascript +var filterMap = require( '@stdlib/ndarray/filter-map' ); +``` + +#### filterMap( x\[, options], fcn\[, thisArg] ) + +Filters and maps elements in an input [ndarray][@stdlib/ndarray/ctor] to elements in a new output [ndarray][@stdlib/ndarray/ctor] according to a callback function. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function fcn( z ) { + if ( z > 5.0 ) { + return z * 10.0; + } +} + +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +var shape = [ 2, 3 ]; +var strides = [ 6, 1 ]; +var offset = 1; + +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +// returns + +var y = filterMap( x, fcn ); +// returns + +var arr = ndarray2array( y ); +// returns [ 80.0, 90.0, 100.0 ] +``` + +The function accepts the following arguments: + +- **x**: input [ndarray][@stdlib/ndarray/ctor]. +- **options**: function options _(optional)_. +- **fcn**: callback function. +- **thisArg**: callback function execution context _(optional)_. + +The function accepts the following options: + +- **dtype**: output ndarray [data type][@stdlib/ndarray/dtypes]. If not specified, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. +- **order**: index iteration order. By default, the function iterates over elements according to the [layout order][@stdlib/ndarray/orders] of the provided [ndarray][@stdlib/ndarray/ctor]. Accordingly, for row-major input [ndarrays][@stdlib/ndarray/ctor], the last dimension indices increment fastest. For column-major input [ndarrays][@stdlib/ndarray/ctor], the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manor, regardless of the input [ndarray][@stdlib/ndarray/ctor]'s layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input [ndarray][@stdlib/ndarray/ctor] may, in some circumstances, result in performance degradation due to cache misses. Must be either `'row-major'` or `'column-major'`. + +By default, the output ndarray [data type][@stdlib/ndarray/dtypes] is inferred from the input [ndarray][@stdlib/ndarray/ctor]. To return an ndarray with a different [data type][@stdlib/ndarray/dtypes], specify the `dtype` option. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var dtype = require( '@stdlib/ndarray/dtype' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function fcn( z ) { + if ( z > 5.0 ) { + return z * 10.0; + } +} + +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +var shape = [ 2, 3 ]; +var strides = [ 6, 1 ]; +var offset = 1; + +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +// returns + +var opts = { + 'dtype': 'float32' +}; +var y = filterMap( x, opts, fcn ); +// returns + +var dt = dtype( y ); +// returns 'float32' + +var arr = ndarray2array( y ); +// returns [ 80.0, 90.0, 100.0 ] +``` + +To set the callback function execution context, provide a `thisArg`. + + + +```javascript +var Float64Array = require( '@stdlib/array/float64' ); +var ndarray = require( '@stdlib/ndarray/ctor' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); + +function fcn( z ) { + this.count += 1; + if ( z > 5.0 ) { + return z * 10.0; + } +} + +var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +var shape = [ 2, 3 ]; +var strides = [ 6, 1 ]; +var offset = 1; + +var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +// returns + +var ctx = { + 'count': 0 +}; +var y = filterMap( x, fcn, ctx ); +// returns + +var arr = ndarray2array( y ); +// returns [ 80.0, 90.0, 100.0 ] + +var count = ctx.count; +// returns 6 +``` + +The callback function is provided the following arguments: + +- **value**: current array element. +- **indices**: current array element indices. +- **arr**: the input [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Notes + +- The function does **not** perform explicit casting (e.g., from a real-valued floating-point number to a complex floating-point number). Any such casting should be performed by a provided callback function. + + + + ```javascript + var Float64Array = require( '@stdlib/array/float64' ); + var ndarray = require( '@stdlib/ndarray/ctor' ); + var Complex128 = require( '@stdlib/complex/float64/ctor' ); + var ndarray2array = require( '@stdlib/ndarray/to-array' ); + + function fcn( z ) { + if ( z > 5.0 ) { + return new Complex128( z, 0.0 ); + } + } + + var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); + var shape = [ 2, 3 ]; + var strides = [ 6, 1 ]; + var offset = 1; + + var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); + // returns + + var opts = { + 'dtype': 'complex128' + }; + var y = filterMap( x, opts, fcn ); + // returns + ``` + +- If a provided callback function returns `undefined`, the function skips the respective [ndarray][@stdlib/ndarray/ctor] element. If the callback function returns a value other than `undefined`, the function stores the callback's return value in the output [ndarray][@stdlib/ndarray/ctor]. + +- The function **always** returns a one-dimensional [ndarray][@stdlib/ndarray/ctor]. + +
+ + + +
+ +## Examples + + + +```javascript +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( '@stdlib/ndarray/to-array' ); +var array = require( '@stdlib/ndarray/array' ); +var filterMap = require( '@stdlib/ndarray/filter-map' ); + +function fcn( v ) { + if ( v > 0 ) { + return v * 100; + } +} + +var buffer = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var x = array( buffer, { + 'shape': [ 5, 2 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = filterMap( x, fcn ); +console.log( ndarray2array( y ) ); +``` + +
+ + + + + + + + + + + + diff --git a/filter-map/benchmark/benchmark.1d.js b/filter-map/benchmark/benchmark.1d.js new file mode 100644 index 00000000..e4e82986 --- /dev/null +++ b/filter-map/benchmark/benchmark.1d.js @@ -0,0 +1,152 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( './../../base/shape2strides' ); +var ndarray = require( './../../ctor' ); +var pkg = require( './../package.json' ).name; +var filterMap = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var ytypes = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} value - array element +* @param {NonNegativeIntegerArray} indices - element indices +* @param {ndarray} arr - input array +* @returns {(number|void)} result +*/ +function fcn( value ) { + if ( value > 0.0 ) { + return value * 10.0; + } +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - array length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} ytype - output ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, ytype, order ) { + var strides; + var opts; + var xbuf; + var x; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + opts = { + 'dtype': ytype + }; + + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var y; + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = filterMap( x, opts, fcn ); + if ( isnan( y.data[ i%y.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var ord; + var sh; + var t1; + var t2; + var f; + var i; + var j; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < xtypes.length; j++ ) { + t1 = xtypes[ j ]; + t2 = ytypes[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len ]; + f = createBenchmark( len, sh, t1, t2, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f ); + } + } + } +} + +main(); diff --git a/filter-map/benchmark/benchmark.2d.js b/filter-map/benchmark/benchmark.2d.js new file mode 100644 index 00000000..0638c3dd --- /dev/null +++ b/filter-map/benchmark/benchmark.2d.js @@ -0,0 +1,163 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isnan = require( '@stdlib/math/base/assert/is-nan' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var pow = require( '@stdlib/math/base/special/pow' ); +var sqrt = require( '@stdlib/math/base/special/sqrt' ); +var floor = require( '@stdlib/math/base/special/floor' ); +var discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var shape2strides = require( './../../base/shape2strides' ); +var ndarray = require( './../../ctor' ); +var pkg = require( './../package.json' ).name; +var filterMap = require( './../lib' ); + + +// VARIABLES // + +var xtypes = [ 'generic' ]; +var ytypes = [ 'float64' ]; +var orders = [ 'row-major', 'column-major' ]; + + +// FUNCTIONS // + +/** +* Callback function. +* +* @private +* @param {number} value - array element +* @param {NonNegativeIntegerArray} indices - element indices +* @param {ndarray} arr - input array +* @returns {(number|void)} result +*/ +function fcn( value ) { + if ( value > 0.0 ) { + return value * 10.0; + } +} + +/** +* Creates a benchmark function. +* +* @private +* @param {PositiveInteger} len - ndarray length +* @param {NonNegativeIntegerArray} shape - ndarray shape +* @param {string} xtype - input ndarray data type +* @param {string} ytype - output ndarray data type +* @param {string} order - ndarray memory layout +* @returns {Function} benchmark function +*/ +function createBenchmark( len, shape, xtype, ytype, order ) { + var strides; + var opts; + var xbuf; + var x; + var y; + + xbuf = discreteUniform( len, -100, 100, { + 'dtype': xtype + }); + strides = shape2strides( shape, order ); + x = ndarray( xtype, xbuf, shape, strides, 0, order ); + opts = { + 'dtype': ytype + }; + return benchmark; + + /** + * Benchmark function. + * + * @private + * @param {Benchmark} b - benchmark instance + */ + function benchmark( b ) { + var i; + + b.tic(); + for ( i = 0; i < b.iterations; i++ ) { + y = filterMap( x, opts, fcn ); + if ( isnan( y.data[ i%y.length ] ) ) { + b.fail( 'should not return NaN' ); + } + } + b.toc(); + if ( !isndarrayLike( y ) ) { + b.fail( 'should return an ndarray' ); + } + b.pass( 'benchmark finished' ); + b.end(); + } +} + + +// MAIN // + +/** +* Main execution sequence. +* +* @private +*/ +function main() { + var len; + var min; + var max; + var ord; + var sh; + var t1; + var t2; + var f; + var i; + var j; + var k; + + min = 1; // 10^min + max = 6; // 10^max + + for ( k = 0; k < orders.length; k++ ) { + ord = orders[ k ]; + for ( j = 0; j < xtypes.length; j++ ) { + t1 = xtypes[ j ]; + t2 = ytypes[ j ]; + for ( i = min; i <= max; i++ ) { + len = pow( 10, i ); + + sh = [ len/2, 2 ]; + f = createBenchmark( len, sh, t1, t2, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f ); + + sh = [ 2, len/2 ]; + f = createBenchmark( len, sh, t1, t2, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f ); + + len = floor( sqrt( len ) ); + sh = [ len, len ]; + len *= len; + f = createBenchmark( len, sh, t1, t2, ord ); + bench( pkg+':ndims='+sh.length+',len='+len+',shape=['+sh.join(',')+'],xorder='+ord+',yorder='+ord+',xtype='+t1+',ytype='+t2, f ); + } + } + } +} + +main(); diff --git a/filter-map/docs/repl.txt b/filter-map/docs/repl.txt new file mode 100644 index 00000000..6777b1f4 --- /dev/null +++ b/filter-map/docs/repl.txt @@ -0,0 +1,56 @@ + +{{alias}}( x[, options], fcn[, thisArg] ) + Filters and maps elements in an input ndarray to elements in a new output + ndarray according to a callback function. + + The callback function is provided the following arguments: + + - value: current array element. + - indices: current array element indices. + - arr: the input ndarray. + + If a provided callback function returns `undefined`, the function skips the + respective ndarray element. If the callback function returns a value other + than `undefined`, the function stores the callback's return value in the + output ndarray. + + Parameters + ---------- + x: ndarray + Input ndarray. + + options: Object (optional) + Function options. + + options.dtype: string (optional) + Output ndarray data type. Overrides using the input array's inferred + data type. + + options.order: string (optional) + Index iteration order. By default, the function iterates over elements + according to the layout order of the provided array. Accordingly, for + row-major input arrays, the last dimension indices increment fastest. + For column-major input arrays, the first dimension indices increment + fastest. To override the inferred order and ensure that indices + increment in a specific manor, regardless of the input array's layout + order, explicitly set the iteration order. Note, however, that iterating + according to an order which does not match that of the input array may, + in some circumstances, result in performance degradation due to cache + misses. Must be either 'row-major' or 'column-major'. + + fcn: Function + Callback function. + + thisArg: any (optional) + Callback function execution context. + + Examples + -------- + > var x = {{alias:@stdlib/ndarray/array}}( [ [ 1.0, 2.0 ], [ 3.0, 4.0 ] ] ); + > function f( v ) { if ( v > 2.0 ) { return v * 10.0; } }; + > var y = {{alias}}( x, f ); + > {{alias:@stdlib/ndarray/to-array}}( y ) + [ 30.0, 40.0 ] + + See Also + -------- diff --git a/filter-map/docs/types/index.d.ts b/filter-map/docs/types/index.d.ts new file mode 100644 index 00000000..18738ec9 --- /dev/null +++ b/filter-map/docs/types/index.d.ts @@ -0,0 +1,1783 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 + +/// + +/* eslint-disable max-lines */ + +import { typedndarray, DataType, Order, float64ndarray, float32ndarray, complex128ndarray, complex64ndarray, int32ndarray, int16ndarray, int8ndarray, uint32ndarray, uint16ndarray, uint8ndarray, uint8cndarray, boolndarray, genericndarray } from '@stdlib/types/ndarray'; +import { Complex64, Complex128, ComplexLike } from '@stdlib/types/complex'; + +/** +* Callback invoked for each ndarray element. +* +* @returns output value +*/ +type Nullary = ( this: V ) => U | void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @returns output value +*/ +type Unary = ( this: V, value: T ) => U | void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @returns output value +*/ +type Binary = ( this: V, value: T, indices: Array ) => U | void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns output value +*/ +type Ternary = ( this: V, value: T, indices: Array, arr: typedndarray ) => U | void; + +/** +* Callback invoked for each ndarray element. +* +* @param value - current array element +* @param indices - current array element indices +* @param arr - input array +* @returns output value +*/ +type Callback = Nullary | Unary | Binary | Ternary; + +/** +* Interface describing "base" function options. +*/ +interface BaseOptions { + /** + * Index iteration order. + * + * ## Notes + * + * - By default, the function iterates over elements according to the layout order of the provided ndarray. Accordingly, for row-major input ndarrays, the last dimension indices increment fastest. For column-major input ndarrays, the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manor, regardless of the input ndarray's layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input ndarray may, in some circumstances, result in performance degradation due to cache misses. + */ + order?: Order; +} + +/** +* Interface describing function options. +*/ +interface OrderOptions { + /** + * Index iteration order. + * + * ## Notes + * + * - By default, the function iterates over elements according to the layout order of the provided ndarray. Accordingly, for row-major input ndarrays, the last dimension indices increment fastest. For column-major input ndarrays, the first dimension indices increment fastest. To override the inferred order and ensure that indices increment in a specific manor, regardless of the input ndarray's layout order, explicitly set the iteration order. Note, however, that iterating according to an order which does not match that of the input ndarray may, in some circumstances, result in performance degradation due to cache misses. + */ + order: Order; +} + +/** +* Interface describing function options. +*/ +interface Options extends BaseOptions { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: DataType; +} + +/** +* Interface describing function options. +*/ +interface Float64Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'float64'; +} + +/** +* Interface describing function options. +*/ +interface Float32Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'float32'; +} + +/** +* Interface describing function options. +*/ +interface Complex128Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'complex128'; +} + +/** +* Interface describing function options. +*/ +interface Complex64Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'complex64'; +} + +/** +* Interface describing function options. +*/ +interface Int32Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'int32'; +} + +/** +* Interface describing function options. +*/ +interface Int16Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'int16'; +} + +/** +* Interface describing function options. +*/ +interface Int8Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'int8'; +} + +/** +* Interface describing function options. +*/ +interface Uint32Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'uint32'; +} + +/** +* Interface describing function options. +*/ +interface Uint16Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'uint16'; +} + +/** +* Interface describing function options. +*/ +interface Uint8Options extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'uint8'; +} + +/** +* Interface describing function options. +*/ +interface Uint8COptions extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'uint8c'; +} + +/** +* Interface describing function options. +*/ +interface BoolOptions extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'bool'; +} + +/** +* Interface describing function options. +*/ +interface GenericOptions extends Options { + /** + * Output ndarray data type. + * + * ## Notes + * + * - This option overrides using the input ndarray's inferred data type. + */ + dtype?: 'generic'; +} + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: float64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: float32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function fcn( z, idx ) { +* if ( idx[ 0 ] > 0 ) { +* return z; +* } +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, fcn ); +* // returns +*/ +declare function filterMap( x: complex64ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function fcn( z, idx ) { +* if ( idx[ 0 ] > 0 ) { +* return z; +* } +* } +* +* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, fcn ); +* // returns +*/ +declare function filterMap( x: complex128ndarray, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: int32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int16Array = require( '@stdlib/array/int16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: int16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Int8Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: int8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint32Array = require( '@stdlib/array/uint32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Uint32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: uint32ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint16Array = require( '@stdlib/array/uint16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Uint16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: uint16ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Uint8Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: uint8ndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: uint8cndarray, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function invert( v ) { +* if ( v ) { +* return !v; +* } +* } +* +* var buffer = new BooleanArray( [ true, true, true, true, true, true, true, true, true, true, true, true ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'bool', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, invert ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ false, false, false ] +*/ +declare function filterMap( x: boolndarray, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10.0; +* } +* } +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: genericndarray, fcn: Callback, thisArg?: ThisParameterType> ): genericndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: float64ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float32Array = require( '@stdlib/array/float32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float32Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: float32ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Complex64Array = require( '@stdlib/array/complex64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function fcn( z, idx ) { +* if ( idx[ 0 ] > 0 ) { +* return z; +* } +* } +* +* var buffer = new Complex64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, fcn ); +* // returns +*/ +declare function filterMap( x: complex64ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Complex128Array = require( '@stdlib/array/complex128' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* +* function fcn( z, idx ) { +* if ( idx[ 0 ] > 0 ) { +* return z; +* } +* } +* +* var buffer = new Complex128Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 3, 1 ]; +* var offset = 0; +* +* var x = ndarray( 'complex128', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, fcn ); +* // returns +*/ +declare function filterMap( x: complex128ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int32Array = require( '@stdlib/array/int32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Int32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: int32ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int16Array = require( '@stdlib/array/int16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Int16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: int16ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Int8Array = require( '@stdlib/array/int8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Int8Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'int8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: int8ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint32Array = require( '@stdlib/array/uint32' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Uint32Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint32', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: uint32ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint16Array = require( '@stdlib/array/uint16' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Uint16Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint16', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: uint16ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint8Array = require( '@stdlib/array/uint8' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Uint8Array( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint8', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: uint8ndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5 ) { +* return z * 10; +* } +* } +* +* var buffer = new Uint8ClampedArray( [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'uint8c', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: uint8cndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var BooleanArray = require( '@stdlib/array/bool' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function invert( v ) { +* if ( v ) { +* return !v; +* } +* } +* +* var buffer = new BooleanArray( [ true, true, true, true, true, true, true, true, true, true, true, true ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'bool', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, invert ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ false, false, false ] +*/ +declare function filterMap( x: boolndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - function options +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ]; +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'generic', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'order': 'row-major' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: genericndarray, options: OrderOptions, fcn: Callback, thisArg?: ThisParameterType> ): genericndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'float64' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: typedndarray, options: Float64Options, fcn: Callback, thisArg?: ThisParameterType> ): float64ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'float32' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: typedndarray, options: Float32Options, fcn: Callback, thisArg?: ThisParameterType> ): float32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Complex128 = require( '@stdlib/complex/float64/ctor' ); +* +* function fcn( z ) { +* if ( idx[ 0 ] > 0 ) { +* return new Complex128( z, 0.0 ); +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'complex128' +* }; +* var y = filterMap( x, opts, fcn ); +* // returns +*/ +declare function filterMap( x: typedndarray, options: Complex128Options, fcn: Callback, thisArg?: ThisParameterType> ): complex128ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var Complex64 = require( '@stdlib/complex/float64/ctor' ); +* +* function fcn( z ) { +* if ( idx[ 0 ] > 0 ) { +* return new Complex64( z, 0.0 ); +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'complex64' +* }; +* var y = filterMap( x, opts, fcn ); +* // returns +*/ +declare function filterMap( x: typedndarray, options: Complex64Options, fcn: Callback, thisArg?: ThisParameterType> ): complex64ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'int32' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: typedndarray, options: Int32Options, fcn: Callback, thisArg?: ThisParameterType> ): int32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'int16' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: typedndarray, options: Int16Options, fcn: Callback, thisArg?: ThisParameterType> ): int16ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'int8' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: typedndarray, options: Int8Options, fcn: Callback, thisArg?: ThisParameterType> ): int8ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'uint32' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: typedndarray, options: Uint32Options, fcn: Callback, thisArg?: ThisParameterType> ): uint32ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'uint16' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: typedndarray, options: Uint16Options, fcn: Callback, thisArg?: ThisParameterType> ): uint16ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'uint8' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: typedndarray, options: Uint8Options, fcn: Callback, thisArg?: ThisParameterType> ): uint8ndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'uint8c' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80, 90, 100 ] +*/ +declare function filterMap( x: typedndarray, options: Uint8COptions, fcn: Callback, thisArg?: ThisParameterType> ): uint8cndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function fcn( z ) { +* if ( z > 5.0 ) { +* return true; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'bool' +* }; +* var y = filterMap( x, opts, fcn ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ true, true, true ] +*/ +declare function filterMap( x: typedndarray, options: BoolOptions, fcn: Callback, thisArg?: ThisParameterType> ): boolndarray; + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param x - input ndarray +* @param options - options +* @param options.dtype - output ndarray data type +* @param options.order - iteration order +* @param fcn - callback function +* @param thisArg - callback function execution context +* @returns output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function scale( z ) { +* if ( z > 5.0 ) { +* return z * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var opts = { +* 'dtype': 'generic' +* }; +* var y = filterMap( x, opts, scale ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +declare function filterMap( x: typedndarray, options: GenericOptions, fcn: Callback, thisArg?: ThisParameterType> ): genericndarray; + + +// EXPORTS // + +export = filterMap; diff --git a/filter-map/docs/types/test.ts b/filter-map/docs/types/test.ts new file mode 100644 index 00000000..25818bea --- /dev/null +++ b/filter-map/docs/types/test.ts @@ -0,0 +1,226 @@ +/* +* @license Apache-2.0 +* +* Copyright (c) 2024 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 { ComplexLike } from '@stdlib/types/complex'; +import empty = require( './../../../base/empty' ); +import zeros = require( './../../../base/zeros' ); +import filterMap = require( './index' ); + +/** +* Callback function. +* +* @param x - input value +* @returns result +*/ +function fcn( x: any ): any { + return x; +} + +/** +* Callback function. +* +* @param x - input value +* @returns result +*/ +function identity1( x: number ): number { + return x; +} + +/** +* Callback function. +* +* @param x - input value +* @returns result +*/ +function identity2( x: boolean ): boolean { + return x; +} + +/** +* Callback function. +* +* @param x - input value +* @returns result +*/ +function identity3( x: ComplexLike ): ComplexLike { + return x; +} + +// The function returns an ndarray... +{ + const sh = [ 2, 2 ]; + const ord = 'row-major'; + + filterMap( zeros( 'float64', sh, ord ), identity1 ); // $ExpectType float64ndarray + filterMap( zeros( 'float64', sh, ord ), identity1, {} ); // $ExpectType float64ndarray + filterMap( zeros( 'float32', sh, ord ), identity1 ); // $ExpectType float32ndarray + filterMap( zeros( 'float32', sh, ord ), identity1, {} ); // $ExpectType float32ndarray + filterMap( zeros( 'complex64', sh, ord ), identity3 ); // $ExpectType complex64ndarray + filterMap( zeros( 'complex64', sh, ord ), identity3, {} ); // $ExpectType complex64ndarray + filterMap( zeros( 'complex128', sh, ord ), identity3 ); // $ExpectType complex128ndarray + filterMap( zeros( 'complex128', sh, ord ), identity3, {} ); // $ExpectType complex128ndarray + filterMap( zeros( 'int32', sh, ord ), identity1 ); // $ExpectType int32ndarray + filterMap( zeros( 'int32', sh, ord ), identity1, {} ); // $ExpectType int32ndarray + filterMap( zeros( 'int16', sh, ord ), identity1 ); // $ExpectType int16ndarray + filterMap( zeros( 'int16', sh, ord ), identity1, {} ); // $ExpectType int16ndarray + filterMap( zeros( 'int8', sh, ord ), identity1 ); // $ExpectType int8ndarray + filterMap( zeros( 'int8', sh, ord ), identity1, {} ); // $ExpectType int8ndarray + filterMap( zeros( 'uint32', sh, ord ), identity1 ); // $ExpectType uint32ndarray + filterMap( zeros( 'uint32', sh, ord ), identity1, {} ); // $ExpectType uint32ndarray + filterMap( zeros( 'uint16', sh, ord ), identity1 ); // $ExpectType uint16ndarray + filterMap( zeros( 'uint16', sh, ord ), identity1, {} ); // $ExpectType uint16ndarray + filterMap( zeros( 'uint8', sh, ord ), identity1 ); // $ExpectType uint8ndarray + filterMap( zeros( 'uint8', sh, ord ), identity1, {} ); // $ExpectType uint8ndarray + filterMap( zeros( 'uint8c', sh, ord ), identity1 ); // $ExpectType uint8cndarray + filterMap( zeros( 'uint8c', sh, ord ), identity1, {} ); // $ExpectType uint8cndarray + filterMap( empty( 'bool', sh, ord ), identity2 ); // $ExpectType boolndarray + filterMap( empty( 'bool', sh, ord ), identity2, {} ); // $ExpectType boolndarray + filterMap( zeros( 'generic', sh, ord ), identity1 ); // $ExpectType genericndarray + filterMap( zeros( 'generic', sh, ord ), identity1, {} ); // $ExpectType genericndarray + + + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'float64' }, fcn ); // $ExpectType float64ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'float64' }, fcn, {} ); // $ExpectType float64ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'float32' }, fcn ); // $ExpectType float32ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'float32' }, fcn, {} ); // $ExpectType float32ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'complex64' }, fcn ); // $ExpectType complex64ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'complex64' }, fcn, {} ); // $ExpectType complex64ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'complex128' }, fcn ); // $ExpectType complex128ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'complex128' }, fcn, {} ); // $ExpectType complex128ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'int32' }, fcn ); // $ExpectType int32ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'int32' }, fcn, {} ); // $ExpectType int32ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'int16' }, fcn ); // $ExpectType int16ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'int16' }, fcn, {} ); // $ExpectType int16ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'int8' }, fcn ); // $ExpectType int8ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'int8' }, fcn, {} ); // $ExpectType int8ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'uint32' }, fcn ); // $ExpectType uint32ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'uint32' }, fcn, {} ); // $ExpectType uint32ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'uint16' }, fcn ); // $ExpectType uint16ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'uint16' }, fcn, {} ); // $ExpectType uint16ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'uint8' }, fcn ); // $ExpectType uint8ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'uint8' }, fcn, {} ); // $ExpectType uint8ndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'uint8c' }, fcn ); // $ExpectType uint8cndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'uint8c' }, fcn, {} ); // $ExpectType uint8cndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'bool' }, fcn ); // $ExpectType boolndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'bool' }, fcn, {} ); // $ExpectType boolndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'generic' }, fcn ); // $ExpectType genericndarray + filterMap( zeros( 'generic', sh, ord ), { 'dtype': 'generic' }, fcn, {} ); // $ExpectType genericndarray + + + filterMap( zeros( 'float64', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType float64ndarray + filterMap( zeros( 'float64', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType float64ndarray + filterMap( zeros( 'float32', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType float32ndarray + filterMap( zeros( 'float32', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType float32ndarray + filterMap( zeros( 'complex64', sh, ord ), { 'order': ord }, identity3 ); // $ExpectType complex64ndarray + filterMap( zeros( 'complex64', sh, ord ), { 'order': ord }, identity3, {} ); // $ExpectType complex64ndarray + filterMap( zeros( 'complex128', sh, ord ), { 'order': ord }, identity3 ); // $ExpectType complex128ndarray + filterMap( zeros( 'complex128', sh, ord ), { 'order': ord }, identity3, {} ); // $ExpectType complex128ndarray + filterMap( zeros( 'int32', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType int32ndarray + filterMap( zeros( 'int32', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType int32ndarray + filterMap( zeros( 'int16', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType int16ndarray + filterMap( zeros( 'int16', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType int16ndarray + filterMap( zeros( 'int8', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType int8ndarray + filterMap( zeros( 'int8', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType int8ndarray + filterMap( zeros( 'uint32', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType uint32ndarray + filterMap( zeros( 'uint32', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType uint32ndarray + filterMap( zeros( 'uint16', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType uint16ndarray + filterMap( zeros( 'uint16', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType uint16ndarray + filterMap( zeros( 'uint8', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType uint8ndarray + filterMap( zeros( 'uint8', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType uint8ndarray + filterMap( zeros( 'uint8c', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType uint8cndarray + filterMap( zeros( 'uint8c', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType uint8cndarray + filterMap( empty( 'bool', sh, ord ), { 'order': ord }, identity2 ); // $ExpectType boolndarray + filterMap( empty( 'bool', sh, ord ), { 'order': ord }, identity2, {} ); // $ExpectType boolndarray + filterMap( zeros( 'generic', sh, ord ), { 'order': ord }, identity1 ); // $ExpectType genericndarray + filterMap( zeros( 'generic', sh, ord ), { 'order': ord }, identity1, {} ); // $ExpectType genericndarray + +} + +// The compiler throws an error if the function is provided a first argument which is not an ndarray... +{ + filterMap( 5, fcn ); // $ExpectError + filterMap( true, fcn ); // $ExpectError + filterMap( false, fcn ); // $ExpectError + filterMap( null, fcn ); // $ExpectError + filterMap( undefined, fcn ); // $ExpectError + filterMap( {}, fcn ); // $ExpectError + filterMap( [ 1 ], fcn ); // $ExpectError + filterMap( ( x: number ): number => x, fcn ); // $ExpectError +} + +// The compiler throws an error if the function is provided a callback which is not a function... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + filterMap( x, '5' ); // $ExpectError + filterMap( x, true ); // $ExpectError + filterMap( x, false ); // $ExpectError + filterMap( x, null ); // $ExpectError + filterMap( x, undefined ); // $ExpectError + filterMap( x, {} ); // $ExpectError + filterMap( x, [ 1 ] ); // $ExpectError +} + +// The compiler throws an error if the function is provided an options argument which is not an object... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + filterMap( x, '10', fcn, {} ); // $ExpectError + filterMap( x, 10, fcn, {} ); // $ExpectError + filterMap( x, false, fcn, {} ); // $ExpectError + filterMap( x, true, fcn, {} ); // $ExpectError + filterMap( x, [], fcn, {} ); // $ExpectError + filterMap( x, ( x: number ): number => x, fcn, {} ); // $ExpectError +} + +// The compiler throws an error if the function is provided a `dtype` option which is not a valid data type... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + filterMap( x, { 'dtype': '10' }, fcn ); // $ExpectError + filterMap( x, { 'dtype': 10 }, fcn ); // $ExpectError + filterMap( x, { 'dtype': null }, fcn ); // $ExpectError + filterMap( x, { 'dtype': false }, fcn ); // $ExpectError + filterMap( x, { 'dtype': true }, fcn ); // $ExpectError + filterMap( x, { 'dtype': [] }, fcn ); // $ExpectError + filterMap( x, { 'dtype': {} }, fcn ); // $ExpectError + filterMap( x, { 'dtype': ( x: number ): number => x }, fcn ); // $ExpectError +} + +// The compiler throws an error if the function is provided an `order` option which is not a valid order... +{ + const x = zeros( 'generic', [ 2, 2 ], 'row-major' ); + + filterMap( x, { 'order': '10' }, fcn ); // $ExpectError + filterMap( x, { 'order': 10 }, fcn ); // $ExpectError + filterMap( x, { 'order': null }, fcn ); // $ExpectError + filterMap( x, { 'order': false }, fcn ); // $ExpectError + filterMap( x, { 'order': true }, fcn ); // $ExpectError + filterMap( x, { 'order': [] }, fcn ); // $ExpectError + filterMap( x, { 'order': {} }, fcn ); // $ExpectError + filterMap( x, { 'order': ( x: number ): number => x }, fcn ); // $ExpectError +} + +// The compiler throws an error if the function is provided an unsupported number of arguments... +{ + filterMap(); // $ExpectError + filterMap( zeros( 'float64', [ 2, 2 ], 'row-major' ) ); // $ExpectError + filterMap( zeros( 'float64', [ 2, 2 ], 'row-major' ), {}, ( x: number ): number => x, {}, {} ); // $ExpectError +} diff --git a/filter-map/examples/index.js b/filter-map/examples/index.js new file mode 100644 index 00000000..b5b5f1cb --- /dev/null +++ b/filter-map/examples/index.js @@ -0,0 +1,42 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 discreteUniform = require( '@stdlib/random/array/discrete-uniform' ); +var ndarray2array = require( './../../to-array' ); +var array = require( './../../array' ); +var filterMap = require( './../lib' ); + +function fcn( v ) { + if ( v > 0 ) { + return v * 100; + } +} + +var buffer = discreteUniform( 10, -100, 100, { + 'dtype': 'generic' +}); +var x = array( buffer, { + 'shape': [ 5, 2 ], + 'dtype': 'generic' +}); +console.log( ndarray2array( x ) ); + +var y = filterMap( x, fcn ); +console.log( ndarray2array( y ) ); diff --git a/filter-map/lib/index.js b/filter-map/lib/index.js new file mode 100644 index 00000000..1acdc0a5 --- /dev/null +++ b/filter-map/lib/index.js @@ -0,0 +1,60 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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'; + +/** +* Filter and map elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @module @stdlib/ndarray/filter-map +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* var filterMap = require( '@stdlib/ndarray/filter-map' ); +* +* function fcn( v ) { +* if ( v > 5.0 ) { +* return v * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, fcn ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ + +// MODULES // + +var main = require( './main.js' ); + + +// EXPORTS // + +module.exports = main; diff --git a/filter-map/lib/main.js b/filter-map/lib/main.js new file mode 100644 index 00000000..bd867af0 --- /dev/null +++ b/filter-map/lib/main.js @@ -0,0 +1,201 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 isPlainObject = require( '@stdlib/assert/is-plain-object' ); +var isFunction = require( '@stdlib/assert/is-function' ); +var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); +var isOrder = require( './../../base/assert/is-order' ); +var hasOwnProp = require( '@stdlib/assert/has-own-property' ); +var ctors = require( './../../base/buffer-ctors' ); +var zeros = require( '@stdlib/array/base/zeros' ); +var getShape = require( './../../shape' ); +var getDType = require( './../../dtype' ); +var getOrder = require( './../../order' ); +var numel = require( './../../base/numel' ); +var nextCartesianIndex = require( './../../base/next-cartesian-index' ).assign; +var gcopy = require( '@stdlib/blas/base/gcopy' ); +var format = require( '@stdlib/string/format' ); + + +// MAIN // + +/** +* Filters and maps elements in an input ndarray to elements in a new output ndarray according to a callback function. +* +* @param {ndarray} x - input ndarray +* @param {Options} [options] - function options +* @param {string} [options.dtype] - output array data type +* @param {boolean} [options.order] - index iteration order +* @param {Callback} fcn - callback function +* @param {*} [thisArg] - callback execution context +* @throws {TypeError} first argument must be an ndarray-like object +* @throws {TypeError} callback argument must be a function +* @throws {TypeError} options argument must be an object +* @returns {ndarray} output ndarray +* +* @example +* var Float64Array = require( '@stdlib/array/float64' ); +* var ndarray = require( '@stdlib/ndarray/ctor' ); +* var ndarray2array = require( '@stdlib/ndarray/to-array' ); +* +* function fcn( v ) { +* if ( v > 5.0 ) { +* return v * 10.0; +* } +* } +* +* var buffer = new Float64Array( [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ] ); +* var shape = [ 2, 3 ]; +* var strides = [ 6, 1 ]; +* var offset = 1; +* +* var x = ndarray( 'float64', buffer, shape, strides, offset, 'row-major' ); +* // returns +* +* var y = filterMap( x, fcn ); +* // returns +* +* var arr = ndarray2array( y ); +* // returns [ 80.0, 90.0, 100.0 ] +*/ +function filterMap( x, options, fcn, thisArg ) { + var hasOpts; + var ndims; + var cache; + var clbk; + var opts; + var ctor; + var ctx; + var ord; + var dim; + var idx; + var buf; + var dt; + var sh; + var N; + var y; + var v; + var i; + if ( !isndarrayLike( x ) ) { + throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) ); + } + if ( arguments.length < 3 ) { + clbk = options; + } else if ( arguments.length === 3 ) { + if ( isFunction( options ) ) { + clbk = options; + ctx = fcn; + } else { + hasOpts = true; + opts = options; + clbk = fcn; + } + } else { + hasOpts = true; + opts = options; + clbk = fcn; + ctx = thisArg; + } + if ( !isFunction( clbk ) ) { + throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', clbk ) ); + } + if ( hasOpts ) { + if ( !isPlainObject( opts ) ) { + throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', opts ) ); + } + if ( hasOwnProp( opts, 'dtype' ) ) { + dt = opts.dtype; + } else { + dt = getDType( x ); + } + if ( hasOwnProp( opts, 'order' ) ) { + if ( !isOrder( opts.order ) ) { + throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', opts.order ) ); + } + ord = opts.order; + } + } else { + dt = getDType( x ); + } + // Resolve an output array buffer constructor: + ctor = ctors( dt ); + if ( ctor === null ) { + // The only way we should get here is if the user provided an unsupported data type, as `getDType` should error if the input array has an unrecognized/unsupported data type... + throw new TypeError( format( 'invalid option. `%s` option must be a recognized data type. Option: `%s`.', 'dtype', opts.dtype ) ); + } + // Resolve the iteration order: + if ( ord === void 0 ) { + ord = getOrder( x ); + } + // Resolve the input array shape: + sh = getShape( x ); + + // Compute the number of array elements: + N = numel( sh ); + + // Retrieve the number of dimensions: + ndims = sh.length; + + // Resolve the dimension in which indices should iterate fastest: + if ( ord === 'row-major' ) { + dim = ndims - 1; + } else { // ord === 'column-major' + dim = 0; + } + // Initialize an index array workspace: + idx = zeros( ndims ); + + // Initialize a value cache for those elements which pass a callback function (note: unfortunately, we use an associative array here, as no other good options. If we use a "generic" array, we are limited to 2^32-1 elements. If we allocate, say, a Float64Array buffer for storing indices, we risk materializing lazily-materialized input ndarray values again (e.g., lazy accessor ndarray), which could be expensive. If we allocate a workspace buffer of equal size to the input ndarray to store materialized values, we'd then need to perform another copy in order to shrink the buffer, as, otherwise, could be holding on to significantly more memory than needed for the returned ndarray. There are likely other options, but all involve complexity, so the simplest option is used here.): + cache = { + 'length': 0 + }; + + // Filter and map elements according to a callback function... + for ( i = 0; i < N; i++ ) { + if ( i > 0 ) { + idx = nextCartesianIndex( sh, ord, idx, dim, idx ); + } + v = clbk.call( ctx, x.get.apply( x, idx ), idx.slice(), x ); + if ( v !== void 0 ) { + cache[ cache.length ] = v; + cache.length += 1; + } + } + // Retrieve the number of cached elements: + N = cache.length; + + // Allocate an output array buffer: + buf = new ctor( N ); + + // Copy cached elements to the output array buffer: + gcopy( N, cache, 1, buf, 1 ); + + // Create an output ndarray: + y = new x.constructor( dt, buf, [ N ], [ 1 ], 0, ord ); + + return y; +} + + +// EXPORTS // + +module.exports = filterMap; diff --git a/filter-map/package.json b/filter-map/package.json new file mode 100644 index 00000000..2393ccf3 --- /dev/null +++ b/filter-map/package.json @@ -0,0 +1,68 @@ +{ + "name": "@stdlib/ndarray/filter-map", + "version": "0.0.0", + "description": "Filter and map elements in an input ndarray to elements in a new output ndarray 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", + "strided", + "array", + "ndarray", + "filter", + "reject", + "extract", + "copy", + "select", + "take", + "map", + "transform", + "for-each", + "apply" + ], + "__stdlib__": {} +} diff --git a/filter-map/test/test.js b/filter-map/test/test.js new file mode 100644 index 00000000..9ac4bb8a --- /dev/null +++ b/filter-map/test/test.js @@ -0,0 +1,924 @@ +/** +* @license Apache-2.0 +* +* Copyright (c) 2024 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 ones = require( '@stdlib/array/ones' ); +var ndarray = require( './../../ctor' ); +var shape2strides = require( './../../base/shape2strides' ); +var strides2offset = require( './../../base/strides2offset' ); +var Float64Array = require( '@stdlib/array/float64' ); +var Float32Array = require( '@stdlib/array/float32' ); +var isSameFloat64Array = require( '@stdlib/assert/is-same-float64array' ); +var isSameFloat32Array = require( '@stdlib/assert/is-same-float32array' ); +var filterMap = require( './../lib' ); + + +// TESTS // + +tape( 'main export is a function', function test( t ) { + t.ok( true, __filename ); + t.strictEqual( typeof filterMap, 'function', 'main export is a function' ); + t.end(); +}); + +tape( 'the function throws an error if provided a first argument which is not an ndarray', function test( t ) { + var values; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [], + {}, + function noop() {}, + { + 'data': true + } + ]; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + filterMap( value, fcn ); + }; + } + + function fcn( z ) { + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function throws an error if a callback argument which is not a function', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + {}, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + filterMap( x, value ); + }; + } +}); + +tape( 'the function throws an error if callback argument which is not a function (options)', function test( t ) { + var values; + var opts; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + {}, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + opts = { + 'dtype': 'float64' + }; + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided ' + values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + filterMap( x, opts, value ); + }; + } +}); + +tape( 'the function throws an error if provided an options argument which is not an object', function test( t ) { + var values; + var x; + var i; + + values = [ + '5', + 5, + true, + false, + null, + void 0, + [] + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + filterMap( x, value, fcn ); + }; + } + + function fcn( z ) { + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function throws an error if provided an invalid `dtype` option', function test( t ) { + var values; + var x; + var i; + + values = [ + 'foo', + 'bar', + 1, + NaN, + true, + false, + void 0, + null, + [], + {}, + function noop() {} + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'dtype': value + }; + filterMap( x, opts, fcn ); + }; + } + + function fcn( z ) { + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function throws an error if provided an invalid `order` option', function test( t ) { + var values; + var x; + var i; + + values = [ + 'foo', + 'bar', + 1, + NaN, + true, + false, + void 0, + null, + [], + {}, + function noop() {} + ]; + x = ndarray( 'generic', ones( 4, 'generic' ), [ 2, 2 ], [ 2, 1 ], 0, 'row-major' ); + + for ( i = 0; i < values.length; i++ ) { + t.throws( badValue( values[i] ), TypeError, 'throws an error when provided '+ values[i] ); + } + t.end(); + + function badValue( value ) { + return function badValue() { + var opts = { + 'order': value + }; + filterMap( x, opts, fcn ); + }; + } + + function fcn( z ) { + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function filters and maps an array according to a callback function (row-major)', function test( t ) { + var expected; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + y = filterMap( x, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function fcn( z ) { + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function filters and maps an array according to a callback function (column-major, contiguous)', function test( t ) { + var expected; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + y = filterMap( x, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function fcn( z ) { + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function filters and maps an array according to a callback function (row-major, contiguous, options)', function test( t ) { + var expected; + var opts; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + x = ndarray( dt, buf, sh, st, o, ord ); + + opts = {}; + y = filterMap( x, opts, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + opts = { + 'dtype': 'float32' + }; + y = filterMap( x, opts, fcn ); + + expected = new Float32Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function fcn( z ) { + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function filters and maps an array according to a callback function (column-major, contiguous, options)', function test( t ) { + var expected; + var opts; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + x = ndarray( dt, buf, sh, st, o, ord ); + + opts = {}; + y = filterMap( x, opts, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + opts = { + 'dtype': 'float32' + }; + y = filterMap( x, opts, fcn ); + + expected = new Float32Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat32Array( y.data, expected ), true, 'returns expected value' ); + + t.end(); + + function fcn( z ) { + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function supports providing a callback execution context', function test( t ) { + var expected; + var ctx; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + ctx = { + 'count': 0 + }; + y = filterMap( x, fcn, ctx ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn( z ) { + this.count += 1; // eslint-disable-line no-invalid-this + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function supports providing a callback execution context (options)', function test( t ) { + var expected; + var ctx; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + ctx = { + 'count': 0 + }; + y = filterMap( x, {}, fcn, ctx ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + t.strictEqual( ctx.count, 4, 'returns expected value' ); + + t.end(); + + function fcn( z ) { + this.count += 1; // eslint-disable-line no-invalid-this + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function invokes a provided callback with three arguments (row-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + var i; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + y = filterMap( x, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x + ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( arrays[ i ], expected[ i ], 'returns expected value' ); + } + + t.end(); + + function fcn( z, idx, arr ) { + values.push( z ); + indices.push( idx ); + arrays.push( arr ); + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function invokes a provided callback with three arguments (column-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + var i; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + y = filterMap( x, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 1, 0, 0 ], + [ 0, 0, 1 ], + [ 1, 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x + ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( arrays[ i ], expected[ i ], 'returns expected value' ); + } + + t.end(); + + function fcn( z, idx, arr ) { + values.push( z ); + indices.push( idx ); + arrays.push( arr ); + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function supports specifying the iteration order (row-major/row-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var opts; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + var i; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + + opts = { + 'order': 'row-major' + }; + y = filterMap( x, opts, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x + ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( arrays[ i ], expected[ i ], 'returns expected value' ); + } + + t.end(); + + function fcn( z, idx, arr ) { + values.push( z ); + indices.push( idx ); + arrays.push( arr ); + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function supports specifying the iteration order (row-major/column-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var opts; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + var i; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'row-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + + opts = { + 'order': 'column-major' + }; + y = filterMap( x, opts, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 1, 0, 0 ], + [ 0, 0, 1 ], + [ 1, 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x + ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( arrays[ i ], expected[ i ], 'returns expected value' ); + } + + t.end(); + + function fcn( z, idx, arr ) { + values.push( z ); + indices.push( idx ); + arrays.push( arr ); + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function supports specifying the iteration order (column-major/row-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var opts; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + var i; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + + opts = { + 'order': 'row-major' + }; + y = filterMap( x, opts, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 0, 0, 1 ], + [ 1, 0, 0 ], + [ 1, 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x + ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( arrays[ i ], expected[ i ], 'returns expected value' ); + } + + t.end(); + + function fcn( z, idx, arr ) { + values.push( z ); + indices.push( idx ); + arrays.push( arr ); + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); + +tape( 'the function supports specifying the iteration order (column-major/column-major)', function test( t ) { + var expected; + var indices; + var values; + var arrays; + var opts; + var ord; + var buf; + var sh; + var st; + var dt; + var o; + var x; + var y; + var i; + + buf = new Float64Array( [ 1.0, -2.0, 3.0, -4.0 ] ); + dt = 'float64'; + ord = 'column-major'; + sh = [ 2, 1, 2 ]; + st = shape2strides( sh, ord ); + o = strides2offset( sh, st ); + + x = ndarray( dt, buf, sh, st, o, ord ); + + values = []; + indices = []; + arrays = []; + + opts = { + 'order': 'column-major' + }; + y = filterMap( x, opts, fcn ); + + expected = new Float64Array([ + 10.0, + 30.0 + ]); + t.strictEqual( isSameFloat64Array( y.data, expected ), true, 'returns expected value' ); + + expected = [ + [ 0, 0, 0 ], + [ 1, 0, 0 ], + [ 0, 0, 1 ], + [ 1, 0, 1 ] + ]; + t.deepEqual( indices, expected, 'returns expected value' ); + + expected = [ + x, + x, + x, + x + ]; + for ( i = 0; i < expected.length; i++ ) { + t.strictEqual( arrays[ i ], expected[ i ], 'returns expected value' ); + } + + t.end(); + + function fcn( z, idx, arr ) { + values.push( z ); + indices.push( idx ); + arrays.push( arr ); + if ( z > 0.0 ) { + return z * 10.0; + } + } +}); diff --git a/filter/lib/main.js b/filter/lib/main.js index 674f5d3e..d385aeba 100644 --- a/filter/lib/main.js +++ b/filter/lib/main.js @@ -79,7 +79,6 @@ function filter( x, options, predicate, thisArg ) { var clbk; var opts; var ctor; - var cidx; var ctx; var ord; var dim; @@ -166,16 +165,14 @@ function filter( x, options, predicate, thisArg ) { }; // Filter elements according to a predicate function... - cidx = -1; for ( i = 0; i < N; i++ ) { if ( i > 0 ) { idx = nextCartesianIndex( sh, ord, idx, dim, idx ); } v = x.get.apply( x, idx ); if ( clbk.call( ctx, v, idx.slice(), x ) ) { + cache[ cache.length ] = v; cache.length += 1; - cidx += 1; - cache[ cidx ] = v; } } // Retrieve the number of cached elements: diff --git a/lib/index.js b/lib/index.js index 41a26821..dd36e14a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -180,6 +180,15 @@ setReadOnly( ns, 'FancyArray', require( './../fancy' ) ); */ setReadOnly( ns, 'filter', require( './../filter' ) ); +/** +* @name filterMap +* @memberof ns +* @readonly +* @type {Function} +* @see {@link module:@stdlib/ndarray/filter-map} +*/ +setReadOnly( ns, 'filterMap', require( './../filter-map' ) ); + /** * @name flag * @memberof ns diff --git a/map/docs/types/index.d.ts b/map/docs/types/index.d.ts index d3ff5e95..3b32038a 100644 --- a/map/docs/types/index.d.ts +++ b/map/docs/types/index.d.ts @@ -633,8 +633,8 @@ declare function map( x: uint8cndarray, fcn: Callback 0 ) { idx = nextCartesianIndex( sh, ord, idx, dim, idx ); } v = x.get.apply( x, idx ); if ( !clbk.call( ctx, v, idx.slice(), x ) ) { + cache[ cache.length ] = v; cache.length += 1; - cidx += 1; - cache[ cidx ] = v; } } // Retrieve the number of cached elements: