@stdlib/utils
Version:
Standard utilities.
107 lines (80 loc) • 2.76 kB
Plain Text
{{alias}}( arr, fcn[, thisArg] )
Applies a function to each element in an array and assigns the result to an
element in a new array.
The applied function is provided the following arguments:
- value: array element.
- index: element index.
- arr: input array.
The returned output array always has a "generic" data type. For example, if
provided an array-like object, the function returns a generic array. If
provided an ndarray, the function returns an ndarray having a "generic" data
type.
Parameters
----------
arr: ArrayLikeObject|ndarray
Input array.
fcn: Function
Function to apply.
thisArg: any (optional)
Input function context.
Returns
-------
out: Array|ndarray
Output array.
Examples
--------
// array-like object:
> var f = {{alias: /utils/nary-function}}( {{alias: /math/base/special/abs}}, 1 );
> var arr = [ -1, -2, -3, -4, -5, -6 ];
> var out = {{alias}}( arr, f )
[ 1, 2, 3, 4, 5, 6 ]
// ndarray:
> arr = {{alias: /ndarray/array}}( arr, { 'shape': [ 2, 3 ] } );
> out = {{alias}}( arr, f );
> var v = out.get( 1, 1 )
5
{{alias}}.assign( arr, out, fcn[, thisArg] )
Applies a function to each element in an array and assigns the result to an
element in an output array.
The applied function is provided the following arguments:
- value: array element.
- index: element index.
- arr: input array.
Input and output arrays must be either both array-like objects or both
ndarray-like objects.
If input and output arrays are array-like objects, the arrays must have the
same number of elements.
If input and output arrays are both ndarray-like objects, the arrays *must*
be broadcast compatible.
Parameters
----------
arr: ArrayLikeObject|ndarray
Input array.
out: ArrayLikeObject|ndarray
Output array.
fcn: Function
Function to apply.
thisArg: any (optional)
Input function context.
Returns
-------
out: Array|ndarray
Output array.
Examples
--------
// array-like object:
> var f = {{alias: /utils/nary-function}}( {{alias: /math/base/special/abs}}, 1 );
> var arr = [ -1, -2, -3, -4, -5, -6 ];
> var out = [ 0, 0, 0, 0, 0, 0 ];
> {{alias}}.assign( arr, out, f );
> out
[ 1, 2, 3, 4, 5, 6 ]
// ndarray:
> var opts = { 'shape': [ 2, 3 ] };
> arr = {{alias: /ndarray/array}}( arr, opts );
> out = {{alias: /ndarray/array}}( [ 0, 0, 0, 0, 0, 0 ], opts );
> {{alias}}.assign( arr, out, f );
> var v = out.get( 1, 1 )
5
See Also
--------