2020
2121/// <reference types="@stdlib/types"/>
2222
23- import { Collection , AccessorArrayLike } from '@stdlib/types/array' ;
23+ import { Collection , AccessorArrayLike , TypedArray } from '@stdlib/types/array' ;
24+
25+ /**
26+ * Input array.
27+ */
28+ type InputArray < T > = Collection < T > | AccessorArrayLike < T > ;
29+
30+ /**
31+ * Output array.
32+ */
33+ type OutputArray < T > = Collection < T > | AccessorArrayLike < T > ;
34+
35+ /**
36+ * Interface for an object having a `map` method.
37+ */
38+ interface ObjectWithMap < T > {
39+ /**
40+ * Invokes a callback for each element in an array and assigns results to a new output array.
41+ *
42+ * @param clbk - callback to apply
43+ * @param thisArg - callback execution context
44+ * @returns output array
45+ */
46+ map < V > ( clbk : Callback < T , InputArray < T > , V , unknown > , thisArg ?: unknown ) : ObjectWithMap < V > ;
47+ }
48+
49+ /**
50+ * An array having a `map` method.
51+ */
52+ type ArrayWithMap < T > = InputArray < T > & ObjectWithMap < T > ;
2453
2554/**
2655* Callback invoked for each array element.
2756*
2857* @returns result
2958*/
30- type Nullary < U , V > = ( this : V ) => U ;
59+ type Nullary < V , ThisArg > = ( this : ThisArg ) => V ;
3160
3261/**
3362* Callback invoked for each array element.
3463*
3564* @param value - current array element
3665* @returns result
3766*/
38- type Unary < T , U , V > = ( this : V , value : T ) => U ;
67+ type Unary < T , V , ThisArg > = ( this : ThisArg , value : T ) => V ;
3968
4069/**
4170* Callback invoked for each array element.
@@ -44,7 +73,7 @@ type Unary<T, U, V> = ( this: V, value: T ) => U;
4473* @param index - current array element index
4574* @returns result
4675*/
47- type Binary < T , U , V > = ( this : V , value : T , index : number ) => U ;
76+ type Binary < T , V , ThisArg > = ( this : ThisArg , value : T , index : number ) => V ;
4877
4978/**
5079* Callback invoked for each array element.
@@ -54,7 +83,7 @@ type Binary<T, U, V> = ( this: V, value: T, index: number ) => U;
5483* @param arr - input array
5584* @returns result
5685*/
57- type Ternary < T , U , V > = ( this : V , value : T , index : number , arr : Collection < T > | AccessorArrayLike < T > ) => U ;
86+ type Ternary < T , U , V , ThisArg > = ( this : ThisArg , value : T , index : number , arr : U ) => V ;
5887
5988/**
6089* Callback invoked for each array element.
@@ -64,7 +93,7 @@ type Ternary<T, U, V> = ( this: V, value: T, index: number, arr: Collection<T> |
6493* @param arr - input array
6594* @returns result
6695*/
67- type Callback < T , U , V > = Nullary < U , V > | Unary < T , U , V > | Binary < T , U , V > | Ternary < T , U , V > ;
96+ type Callback < T , U , V , ThisArg > = Nullary < V , ThisArg > | Unary < T , V , ThisArg > | Binary < T , V , ThisArg > | Ternary < T , U , V , ThisArg > ;
6897
6998/**
7099* Interface describing the main export.
@@ -79,23 +108,51 @@ interface Routine {
79108 * @returns output array
80109 *
81110 * @example
82- * var toAccessorArray = require( '@stdlib/array/to-accessor-array' );
83111 * var ones = require( '@stdlib/array/base/ones' );
84112 *
85113 * function scale( v ) {
86- * return v * 10.0 ;
114+ * return v * 10;
87115 * }
88116 *
89117 * var x = ones( 4 );
90- * var y = map( toAccessorArray( x ), scale );
91- * // returns [ 10.0, 10.0, 10.0, 10.0 ]
118+ * // returns [ 0, 0, 0, 0 ]
119+ *
120+ * var y = map( x, scale );
121+ * // returns [ 10, 10, 10, 10 ]
122+ */
123+ < T = unknown , U extends Array < T > = Array < T > , V = unknown , W extends Array < V > = Array < V > , ThisArg = unknown > ( x : U , fcn : Callback < T , U , V , ThisArg > , thisArg ?: ThisParameterType < Callback < T , U , V , ThisArg > > ) : W ;
124+
125+ /**
126+ * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
127+ *
128+ * @param x - input array object
129+ * @param fcn - callback function
130+ * @param thisArg - callback execution context
131+ * @returns output array
132+ *
133+ * @example
134+ * var ones = require( '@stdlib/array/ones' );
135+ *
136+ * function scale( v ) {
137+ * return v * 10;
138+ * }
139+ *
140+ * var x = ones( 4, 'int32' );
141+ * // returns <Int32Array>[ 0, 0, 0, 0 ]
142+ *
143+ * var y = map( x, scale );
144+ * // returns <Int32Array>[ 10, 10, 10, 10 ]
92145 */
93- < T = unknown , U = unknown , V = unknown > ( x : AccessorArrayLike < T > , fcn : Callback < T , U , V > , thisArg ?: ThisParameterType < Callback < T , U , V > > ) : AccessorArrayLike < U > ;
146+ < T = unknown , U extends TypedArray = TypedArray , V = unknown , ThisArg = unknown > ( x : U , fcn : Callback < T , U , V , ThisArg > , thisArg ?: ThisParameterType < Callback < T , U , V , ThisArg > > ) : U ;
94147
95148 /**
96149 * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
97150 *
98- * @param x - input array
151+ * ## Notes
152+ *
153+ * - We assume that an input array having a `map` method returns an array of the same class, and, thus, the output array should also have a `map` method.
154+ *
155+ * @param x - input array object
99156 * @param fcn - callback function
100157 * @param thisArg - callback execution context
101158 * @returns output array
@@ -104,52 +161,55 @@ interface Routine {
104161 * var ones = require( '@stdlib/array/base/ones' );
105162 *
106163 * function scale( v ) {
107- * return v * 10.0 ;
164+ * return v * 10;
108165 * }
109166 *
110167 * var x = ones( 4 );
168+ * // returns [ 0, 0, 0, 0 ]
169+ *
111170 * var y = map( x, scale );
112- * // returns [ 10.0 , 10.0 , 10.0 , 10.0 ]
171+ * // returns [ 10, 10, 10, 10 ]
113172 */
114- < T = unknown , U = unknown , V = unknown > ( x : Collection < T > , fcn : Callback < T , U , V > , thisArg ?: ThisParameterType < Callback < T , U , V > > ) : Collection < U > ;
173+ < T = unknown , U extends ArrayWithMap < T > = ArrayWithMap < T > , V = unknown , W extends ArrayWithMap < V > = ArrayWithMap < V > , ThisArg = unknown > ( x : U , fcn : Callback < T , U , V , ThisArg > , thisArg ?: ThisParameterType < Callback < T , U , V , ThisArg > > ) : W ;
115174
116175 /**
117- * Applies a callback function to elements in an input array and assigns results to elements in an output array.
176+ * Applies a callback function to elements in an input array and assigns results to elements in a new output array.
177+ *
178+ * ## Notes
179+ *
180+ * - When an input array does not have a `map` method, we always return a new "generic" array.
118181 *
119182 * @param x - input array object
120- * @param y - output array object
121- * @param stride - stride length for output array
122- * @param offset - starting index for output array
123183 * @param fcn - callback function
124184 * @param thisArg - callback execution context
125- * @returns output array object
185+ * @returns output array
126186 *
127187 * @example
128188 * var toAccessorArray = require( '@stdlib/array/to-accessor-array' );
129189 * var ones = require( '@stdlib/array/base/ones' );
130- * var zeros = require( '@stdlib/array/base/zeros' );
131190 *
132191 * function scale( v ) {
133192 * return v * 10;
134193 * }
194+ *
135195 * var x = ones( 4 );
136- * var y = zeros( x.length );
196+ * // returns [ 0, 0, 0, 0 ]
137197 *
138- * var out = map.assign ( toAccessorArray( x ), toAccessorArray( y ), 1, 0 scale );
139- * // y => [ 10, 10, 10, 10 ]
198+ * var y = map( toAccessorArray( x ), scale );
199+ * // returns [ 10, 10, 10, 10 ]
140200 */
141- assign < T = unknown , U = unknown , V = unknown > ( x : AccessorArrayLike < T > , y : AccessorArrayLike < U > , stride : number , offset : number , fcn : Callback < T , U , V > , thisArg ?: ThisParameterType < Callback < T , U , V > > ) : AccessorArrayLike < U > ;
201+ < T = unknown , U extends InputArray < T > = InputArray < T > , V = unknown , ThisArg = unknown > ( x : U , fcn : Callback < T , U , V , ThisArg > , thisArg ?: ThisParameterType < Callback < T , U , V , ThisArg > > ) : Array < V > ;
142202
143203 /**
144204 * Applies a callback function to elements in an input array and assigns results to elements in an output array.
145205 *
146- * @param x - input array
147- * @param y - output array
206+ * @param x - input array object
207+ * @param y - output array object
148208 * @param stride - stride length for output array
149209 * @param offset - starting index for output array
150210 * @param fcn - callback function
151211 * @param thisArg - callback execution context
152- * @returns output array
212+ * @returns output array object
153213 *
154214 * @example
155215 * var ones = require( '@stdlib/array/base/ones' );
@@ -166,8 +226,22 @@ interface Routine {
166226 *
167227 * var bool = ( out === y );
168228 * // returns true
229+ *
230+ * @example
231+ * var toAccessorArray = require( '@stdlib/array/to-accessor-array' );
232+ * var ones = require( '@stdlib/array/base/ones' );
233+ * var zeros = require( '@stdlib/array/base/zeros' );
234+ *
235+ * function scale( v ) {
236+ * return v * 10;
237+ * }
238+ * var x = ones( 4 );
239+ * var y = zeros( x.length );
240+ *
241+ * var out = map.assign( toAccessorArray( x ), toAccessorArray( y ), 1, 0 scale );
242+ * // y => [ 10, 10, 10, 10 ]
169243 */
170- assign < T = unknown , U = unknown , V = unknown > ( x : Collection < T > , y : Collection < U > , stride : number , offset : number , fcn : Callback < T , U , V > , thisArg ?: ThisParameterType < Callback < T , U , V > > ) : Collection < U > ;
244+ assign < T = unknown , U extends InputArray < T > = InputArray < T > , V = unknown , W extends OutputArray < V > = OutputArray < V > , ThisArg = unknown > ( x : U , y : W , stride : number , offset : number , fcn : Callback < T , U , V , ThisArg > , thisArg ?: ThisParameterType < Callback < T , U , V , ThisArg > > ) : W ;
171245}
172246
173247/**
0 commit comments