Skip to content

Commit 959a92d

Browse files
committed
refactor: apply suggestions from code review
1 parent 49533b7 commit 959a92d

File tree

6 files changed

+103
-44
lines changed

6 files changed

+103
-44
lines changed

lib/node_modules/@stdlib/ndarray/flatten-by/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ limitations under the License.
2020

2121
# flattenBy
2222

23-
> Apply a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and return a flattened output [ndarray][@stdlib/ndarray/ctor].
23+
> Flatten an [ndarray][@stdlib/ndarray/ctor] according to a callback function.
2424
2525
<section class="intro">
2626

@@ -38,7 +38,7 @@ var flattenBy = require( '@stdlib/ndarray/flatten-by' );
3838

3939
#### flattenBy( x\[, options], fcn[, thisArg] )
4040

41-
Applies a callback to elements in an input [ndarray][@stdlib/ndarray/ctor] and returns a flattened output [ndarray][@stdlib/ndarray/ctor].
41+
Flattens an [ndarray][@stdlib/ndarray/ctor] according to a callback function.
4242

4343
```javascript
4444
var array = require( '@stdlib/ndarray/array' );

lib/node_modules/@stdlib/ndarray/flatten-by/docs/repl.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
{{alias}}( x[, options], fcn[, thisArg] )
3-
Applies a callback to elements in an input ndarray and returns a flattened
4-
output ndarray.
3+
Flattens an ndarray according to a callback function.
54

65
Parameters
76
----------

lib/node_modules/@stdlib/ndarray/flatten-by/docs/types/index.d.ts

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ interface Options {
100100
}
101101

102102
/**
103-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
103+
* Flattens an ndarray according to a callback function.
104104
*
105105
* @param x - input ndarray
106106
* @param fcn - callback function
@@ -133,7 +133,7 @@ interface Options {
133133
declare function flattenBy<W = unknown>( x: float64ndarray, fcn: Callback<number, float64ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, float64ndarray, number, W>> ): float64ndarray;
134134

135135
/**
136-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
136+
* Flattens an ndarray according to a callback function.
137137
*
138138
* @param x - input ndarray
139139
* @param fcn - callback function
@@ -166,7 +166,7 @@ declare function flattenBy<W = unknown>( x: float64ndarray, fcn: Callback<number
166166
declare function flattenBy<W = unknown>( x: float32ndarray, fcn: Callback<number, float32ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, float32ndarray, number, W>> ): float32ndarray;
167167

168168
/**
169-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
169+
* Flattens an ndarray according to a callback function.
170170
*
171171
* @param x - input ndarray
172172
* @param fcn - callback function
@@ -196,7 +196,7 @@ declare function flattenBy<W = unknown>( x: float32ndarray, fcn: Callback<number
196196
declare function flattenBy<W = unknown>( x: complex64ndarray, fcn: Callback<Complex64, complex64ndarray, ComplexLike, W>, thisArg?: ThisParameterType<Callback<Complex64, complex64ndarray, ComplexLike, W>> ): complex64ndarray;
197197

198198
/**
199-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
199+
* Flattens an ndarray according to a callback function.
200200
*
201201
* @param x - input ndarray
202202
* @param fcn - callback function
@@ -226,7 +226,7 @@ declare function flattenBy<W = unknown>( x: complex64ndarray, fcn: Callback<Comp
226226
declare function flattenBy<W = unknown>( x: complex128ndarray, fcn: Callback<Complex128, complex128ndarray, ComplexLike, W>, thisArg?: ThisParameterType<Callback<Complex128, complex128ndarray, ComplexLike, W>> ): complex128ndarray;
227227

228228
/**
229-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
229+
* Flattens an ndarray according to a callback function.
230230
*
231231
* @param x - input ndarray
232232
* @param fcn - callback function
@@ -259,7 +259,7 @@ declare function flattenBy<W = unknown>( x: complex128ndarray, fcn: Callback<Com
259259
declare function flattenBy<W = unknown>( x: int32ndarray, fcn: Callback<number, int32ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, int32ndarray, number, W>> ): int32ndarray;
260260

261261
/**
262-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
262+
* Flattens an ndarray according to a callback function.
263263
*
264264
* @param x - input ndarray
265265
* @param fcn - callback function
@@ -292,7 +292,7 @@ declare function flattenBy<W = unknown>( x: int32ndarray, fcn: Callback<number,
292292
declare function flattenBy<W = unknown>( x: int16ndarray, fcn: Callback<number, int16ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, int16ndarray, number, W>> ): int16ndarray;
293293

294294
/**
295-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
295+
* Flattens an ndarray according to a callback function.
296296
*
297297
* @param x - input ndarray
298298
* @param fcn - callback function
@@ -325,7 +325,7 @@ declare function flattenBy<W = unknown>( x: int16ndarray, fcn: Callback<number,
325325
declare function flattenBy<W = unknown>( x: int8ndarray, fcn: Callback<number, int8ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, int8ndarray, number, W>> ): int8ndarray;
326326

327327
/**
328-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
328+
* Flattens an ndarray according to a callback function.
329329
*
330330
* @param x - input ndarray
331331
* @param fcn - callback function
@@ -358,7 +358,7 @@ declare function flattenBy<W = unknown>( x: int8ndarray, fcn: Callback<number, i
358358
declare function flattenBy<W = unknown>( x: uint32ndarray, fcn: Callback<number, uint32ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, uint32ndarray, number, W>> ): uint32ndarray;
359359

360360
/**
361-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
361+
* Flattens an ndarray according to a callback function.
362362
*
363363
* @param x - input ndarray
364364
* @param fcn - callback function
@@ -391,7 +391,7 @@ declare function flattenBy<W = unknown>( x: uint32ndarray, fcn: Callback<number,
391391
declare function flattenBy<W = unknown>( x: uint16ndarray, fcn: Callback<number, uint16ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, uint16ndarray, number, W>> ): uint16ndarray;
392392

393393
/**
394-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
394+
* Flattens an ndarray according to a callback function.
395395
*
396396
* @param x - input ndarray
397397
* @param fcn - callback function
@@ -424,7 +424,7 @@ declare function flattenBy<W = unknown>( x: uint16ndarray, fcn: Callback<number,
424424
declare function flattenBy<W = unknown>( x: uint8ndarray, fcn: Callback<number, uint8ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, uint8ndarray, number, W>> ): uint8ndarray;
425425

426426
/**
427-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
427+
* Flattens an ndarray according to a callback function.
428428
*
429429
* @param x - input ndarray
430430
* @param fcn - callback function
@@ -457,7 +457,7 @@ declare function flattenBy<W = unknown>( x: uint8ndarray, fcn: Callback<number,
457457
declare function flattenBy<W = unknown>( x: uint8cndarray, fcn: Callback<number, uint8cndarray, number, W>, thisArg?: ThisParameterType<Callback<number, uint8cndarray, number, W>> ): uint8cndarray;
458458

459459
/**
460-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
460+
* Flattens an ndarray according to a callback function.
461461
*
462462
* @param x - input ndarray
463463
* @param fcn - callback function
@@ -490,7 +490,7 @@ declare function flattenBy<W = unknown>( x: uint8cndarray, fcn: Callback<number,
490490
declare function flattenBy<W = unknown>( x: boolndarray, fcn: Callback<boolean, boolndarray, boolean, W>, thisArg?: ThisParameterType<Callback<boolean, boolndarray, boolean, W>> ): boolndarray;
491491

492492
/**
493-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
493+
* Flattens an ndarray according to a callback function.
494494
*
495495
* @param x - input ndarray
496496
* @param fcn - callback function
@@ -522,7 +522,7 @@ declare function flattenBy<W = unknown>( x: boolndarray, fcn: Callback<boolean,
522522
declare function flattenBy<T = unknown, V = unknown, W = unknown>( x: genericndarray<T>, fcn: Callback<T, genericndarray<T>, V, W>, thisArg?: ThisParameterType<Callback<T, genericndarray<T>, V, W>> ): genericndarray<V>;
523523

524524
/**
525-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
525+
* Flattens an ndarray according to a callback function.
526526
*
527527
* @param x - input ndarray
528528
* @param options - function options
@@ -562,7 +562,7 @@ declare function flattenBy<T = unknown, V = unknown, W = unknown>( x: genericnda
562562
declare function flattenBy<W = unknown>( x: float64ndarray, options: Options, fcn: Callback<number, float64ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, float64ndarray, number, W>> ): float64ndarray;
563563

564564
/**
565-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
565+
* Flattens an ndarray according to a callback function.
566566
*
567567
* @param x - input ndarray
568568
* @param options - function options
@@ -602,7 +602,7 @@ declare function flattenBy<W = unknown>( x: float64ndarray, options: Options, fc
602602
declare function flattenBy<W = unknown>( x: float32ndarray, options: Options, fcn: Callback<number, float32ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, float32ndarray, number, W>> ): float32ndarray;
603603

604604
/**
605-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
605+
* Flattens an ndarray according to a callback function.
606606
*
607607
* @param x - input ndarray
608608
* @param options - function options
@@ -639,7 +639,7 @@ declare function flattenBy<W = unknown>( x: float32ndarray, options: Options, fc
639639
declare function flattenBy<W = unknown>( x: complex64ndarray, options: Options, fcn: Callback<Complex64, complex64ndarray, ComplexLike, W>, thisArg?: ThisParameterType<Callback<Complex64, complex64ndarray, ComplexLike, W>> ): complex64ndarray;
640640

641641
/**
642-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
642+
* Flattens an ndarray according to a callback function.
643643
*
644644
* @param x - input ndarray
645645
* @param options - function options
@@ -676,7 +676,7 @@ declare function flattenBy<W = unknown>( x: complex64ndarray, options: Options,
676676
declare function flattenBy<W = unknown>( x: complex128ndarray, options: Options, fcn: Callback<Complex128, complex128ndarray, ComplexLike, W>, thisArg?: ThisParameterType<Callback<Complex128, complex128ndarray, ComplexLike, W>> ): complex128ndarray;
677677

678678
/**
679-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
679+
* Flattens an ndarray according to a callback function.
680680
*
681681
* @param x - input ndarray
682682
* @param options - function options
@@ -716,7 +716,7 @@ declare function flattenBy<W = unknown>( x: complex128ndarray, options: Options,
716716
declare function flattenBy<W = unknown>( x: int32ndarray, options: Options, fcn: Callback<number, int32ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, int32ndarray, number, W>> ): int32ndarray;
717717

718718
/**
719-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
719+
* Flattens an ndarray according to a callback function.
720720
*
721721
* @param x - input ndarray
722722
* @param options - function options
@@ -756,7 +756,7 @@ declare function flattenBy<W = unknown>( x: int32ndarray, options: Options, fcn:
756756
declare function flattenBy<W = unknown>( x: int16ndarray, options: Options, fcn: Callback<number, int16ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, int16ndarray, number, W>> ): int16ndarray;
757757

758758
/**
759-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
759+
* Flattens an ndarray according to a callback function.
760760
*
761761
* @param x - input ndarray
762762
* @param options - function options
@@ -796,7 +796,7 @@ declare function flattenBy<W = unknown>( x: int16ndarray, options: Options, fcn:
796796
declare function flattenBy<W = unknown>( x: int8ndarray, options: Options, fcn: Callback<number, int8ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, int8ndarray, number, W>> ): int8ndarray;
797797

798798
/**
799-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
799+
* Flattens an ndarray according to a callback function.
800800
*
801801
* @param x - input ndarray
802802
* @param options - function options
@@ -836,7 +836,7 @@ declare function flattenBy<W = unknown>( x: int8ndarray, options: Options, fcn:
836836
declare function flattenBy<W = unknown>( x: uint32ndarray, options: Options, fcn: Callback<number, uint32ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, uint32ndarray, number, W>> ): uint32ndarray;
837837

838838
/**
839-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
839+
* Flattens an ndarray according to a callback function.
840840
*
841841
* @param x - input ndarray
842842
* @param options - function options
@@ -876,7 +876,7 @@ declare function flattenBy<W = unknown>( x: uint32ndarray, options: Options, fcn
876876
declare function flattenBy<W = unknown>( x: uint16ndarray, options: Options, fcn: Callback<number, uint16ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, uint16ndarray, number, W>> ): uint16ndarray;
877877

878878
/**
879-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
879+
* Flattens an ndarray according to a callback function.
880880
*
881881
* @param x - input ndarray
882882
* @param options - function options
@@ -916,7 +916,7 @@ declare function flattenBy<W = unknown>( x: uint16ndarray, options: Options, fcn
916916
declare function flattenBy<W = unknown>( x: uint8ndarray, options: Options, fcn: Callback<number, uint8ndarray, number, W>, thisArg?: ThisParameterType<Callback<number, uint8ndarray, number, W>> ): uint8ndarray;
917917

918918
/**
919-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
919+
* Flattens an ndarray according to a callback function.
920920
*
921921
* @param x - input ndarray
922922
* @param options - function options
@@ -956,7 +956,7 @@ declare function flattenBy<W = unknown>( x: uint8ndarray, options: Options, fcn:
956956
declare function flattenBy<W = unknown>( x: uint8cndarray, options: Options, fcn: Callback<number, uint8cndarray, number, W>, thisArg?: ThisParameterType<Callback<number, uint8cndarray, number, W>> ): uint8cndarray;
957957

958958
/**
959-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
959+
* Flattens an ndarray according to a callback function.
960960
*
961961
* @param x - input ndarray
962962
* @param options - function options
@@ -996,7 +996,7 @@ declare function flattenBy<W = unknown>( x: uint8cndarray, options: Options, fcn
996996
declare function flattenBy<W = unknown>( x: boolndarray, options: Options, fcn: Callback<boolean, boolndarray, boolean, W>, thisArg?: ThisParameterType<Callback<boolean, boolndarray, boolean, W>> ): boolndarray;
997997

998998
/**
999-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
999+
* Flattens an ndarray according to a callback function.
10001000
*
10011001
* @param x - input ndarray
10021002
* @param options - function options

lib/node_modules/@stdlib/ndarray/flatten-by/lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
'use strict';
2020

2121
/**
22-
* Apply a callback to elements in an input ndarray and return a flattened output ndarray.
22+
* Flatten an ndarray according to a callback function.
2323
*
2424
* @module @stdlib/ndarray/flatten-by
25-
*
25+
*s
2626
* @example
2727
* var array = require( '@stdlib/ndarray/array' );
2828
* var ndarray2array = require( '@stdlib/ndarray/to-array' );

lib/node_modules/@stdlib/ndarray/flatten-by/lib/main.js

Lines changed: 71 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,28 @@
2323
var isPlainObject = require( '@stdlib/assert/is-plain-object' );
2424
var isFunction = require( '@stdlib/assert/is-function' );
2525
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
26-
var map = require( '@stdlib/ndarray/map' );
27-
var flatten = require( '@stdlib/ndarray/flatten' );
26+
var hasOwnProp = require( '@stdlib/assert/has-own-property' );
27+
var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' );
28+
var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' );
29+
var isOrder = require( '@stdlib/ndarray/base/assert/is-order' );
30+
var getShape = require( '@stdlib/ndarray/shape' );
31+
var getOrder = require( '@stdlib/ndarray/order' );
32+
var getStrides = require( '@stdlib/ndarray/strides' );
33+
var getData = require( '@stdlib/ndarray/base/data-buffer' );
34+
var getDType = require( '@stdlib/ndarray/base/dtype' );
35+
var shape2strides = require( '@stdlib/ndarray/base/shape2strides' );
36+
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
37+
var flattenShape = require( '@stdlib/ndarray/base/flatten-shape' );
38+
var map = require( '@stdlib/ndarray/base/map' );
39+
var emptyLike = require( '@stdlib/ndarray/empty-like' );
40+
var ndarray = require( '@stdlib/ndarray/ctor' );
2841
var format = require( '@stdlib/string/format' );
2942

3043

3144
// MAIN //
3245

3346
/**
34-
* Applies a callback to elements in an input ndarray and returns a flattened output ndarray.
47+
* Flattens an ndarray according to a callback function.
3548
*
3649
* @param {ndarray} x - input ndarray
3750
* @param {Options} [options] - function options
@@ -71,33 +84,80 @@ function flattenBy( x, options, fcn, thisArg ) {
7184
if ( !isndarrayLike( x ) ) {
7285
throw new TypeError( format( 'invalid argument. First argument must be an ndarray-like object. Value: `%s`.', x ) );
7386
}
74-
opts = {};
75-
if ( arguments.length < 3 ) { // Case: flattenBy( x, fcn )
87+
nargs = arguments.length;
88+
xsh = getShape( x );
89+
90+
// Define default options:
91+
opts = {
92+
'depth': xsh.length, // by default, flatten to a one-dimensional ndarray
93+
'order': ROW_MAJOR // by default, flatten in lexicographic order (i.e., trailing dimensions first; e.g., if `x` is a matrix, flatten row-by-row)
94+
};
95+
96+
if ( nargs <= 2 ) { // Case: flattenBy( x, fcn )
7697
cb = options;
77-
} else if ( arguments.length === 3 ) {
98+
} else if ( nargs <= 3 ) {
7899
if ( isFunction( options ) ) { // Case: flattenBy( x, fcn, thisArg )
79100
cb = options;
80101
ctx = fcn;
81102
} else { // Case: flattenBy( x, options, fcn )
82103
if ( !isPlainObject( options ) ) {
83104
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
84105
}
85-
opts = options;
86106
cb = fcn;
87107
}
88-
} else {
108+
} else { // Case: flattenBy( x, options, fcn, thisArg )
89109
if ( !isPlainObject( options ) ) {
90110
throw new TypeError( format( 'invalid argument. Options argument must be an object. Value: `%s`.', options ) );
91111
}
92-
opts = options;
93112
cb = fcn;
94113
ctx = thisArg;
95114
}
96115
if ( !isFunction( cb ) ) {
97116
throw new TypeError( format( 'invalid argument. Callback argument must be a function. Value: `%s`.', cb ) );
98117
}
99-
y = map( x, cb, ctx );
100-
return flatten( y, opts );
118+
if ( hasOwnProp( options, 'depth' ) ) {
119+
if ( !isNonNegativeInteger( options.depth ) ) {
120+
throw new TypeError( format( 'invalid option. `%s` option must be a nonnegative integer. Option: `%s`.', options.depth ) );
121+
}
122+
opts.depth = options.depth;
123+
}
124+
if ( hasOwnProp( options, 'order' ) ) {
125+
if ( options.order === 'any' ) {
126+
// When 'any', we want to flatten according to the physical layout of the data in memory...
127+
o = strides2order( getStrides( x ) );
128+
if ( o === 1 ) {
129+
// Data is currently arranged in row-major order:
130+
opts.order = ROW_MAJOR;
131+
} else if ( o === 2 ) {
132+
// Data is currently arranged in column-major order:
133+
opts.order = COL_MAJOR;
134+
} else { // o === 0 || o === 3 (i.e., neither row- nor column-major || both row- and column-major
135+
// When the data is either both row- and column-major (e.g., a one-dimensional ndarray) or neither row- nor column-major (e.g., unordered strides), fallback to flattening according to the stated order of the input ndarray:
136+
opts.order = getOrder( x );
137+
}
138+
} else if ( options.order === 'same' ) {
139+
// When 'same', we want to flatten according to the stated order of the input ndarray:
140+
opts.order = getOrder( x );
141+
} else if ( isOrder( options.order ) ) {
142+
// When provided a specific order, flatten according to that order regardless of the order of the input ndarray:
143+
opts.order = options.order;
144+
} else {
145+
throw new TypeError( format( 'invalid option. `%s` option must be a recognized order. Option: `%s`.', 'order', options.order ) );
146+
}
147+
}
148+
// Create an output ndarray having contiguous memory:
149+
y = emptyLike( x, {
150+
'shape': flattenShape( xsh, opts.depth ),
151+
'order': opts.order
152+
});
153+
154+
// Create a view on top of output ndarray having the same shape as the input ndarray:
155+
st = ( xsh.length > 0 ) ? shape2strides( xsh, opts.order ) : [ 0 ];
156+
view = ndarray( getDType( y ), getData( y ), xsh, st, 0, opts.order );
157+
158+
// Transform and assign elements to the output ndarray:
159+
map( [ x, view ], cb, ctx );
160+
return y;
101161
}
102162

103163

0 commit comments

Comments
 (0)