Skip to content

Commit 97d0e24

Browse files
committed
feat: add array/base/flatten-by
1 parent d940344 commit 97d0e24

File tree

11 files changed

+2836
-0
lines changed

11 files changed

+2836
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2023 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
<!-- lint disable maximum-heading-length -->
22+
23+
# flattenBy
24+
25+
> Flatten an n-dimensional nested array according to a callback function.
26+
27+
<section class="usage">
28+
29+
## Usage
30+
31+
```javascript
32+
var flattenBy = require( '@stdlib/array/base/flatten-by' );
33+
```
34+
35+
#### flattenBy( x, shape, colexicographic, clbk\[, thisArg] )
36+
37+
Flattens an n-dimensional nested array according to a callback function.
38+
39+
```javascript
40+
function scale( v ) {
41+
return v * 2;
42+
}
43+
44+
var x = [ [ 1, 2 ], [ 3, 4 ] ];
45+
46+
var out = flattenBy( x, [ 2, 2 ], false, scale );
47+
// returns [ 2, 4, 6, 8 ]
48+
```
49+
50+
To flatten in colexicographic order, provide a third argument equal to `true`.
51+
52+
```javascript
53+
function scale( v ) {
54+
return v * 2;
55+
}
56+
57+
var x = [ [ 1, 2 ], [ 3, 4 ] ];
58+
59+
var out = flattenBy( x, [ 2, 2 ], true, scale );
60+
// returns [ 2, 6, 4, 8 ]
61+
```
62+
63+
To set the callback execution context, provide a `thisArg` argument.
64+
65+
<!-- eslint-disable no-invalid-this -->
66+
67+
```javascript
68+
function scale( v ) {
69+
this.count += 1;
70+
return v * 2;
71+
}
72+
73+
var x = [ [ 1, 2 ], [ 3, 4 ] ];
74+
var ctx = {
75+
'count': 0
76+
};
77+
78+
var out = flattenBy( x, [ 2, 2 ], false, scale, ctx );
79+
// returns [ 2, 4, 6, 8 ]
80+
81+
var count = ctx.count;
82+
// returns 4
83+
```
84+
85+
#### flattenBy.assign( x, shape, colexicographic, out, stride, offset, clbk\[, thisArg] )
86+
87+
Flattens an n-dimensional nested array according to a callback function and assigns elements to a provided output array.
88+
89+
```javascript
90+
var Float64Array = require( '@stdlib/array/float64' );
91+
92+
function scale( v ) {
93+
return v * 2;
94+
}
95+
96+
var x = [ [ 1, 2 ], [ 3, 4 ] ];
97+
var out = new Float64Array( 4 );
98+
99+
var y = flattenBy.assign( x, [ 2, 2 ], false, out, 1, 0, scale );
100+
// returns <Float64Array>[ 2, 4, 6, 8 ]
101+
102+
var bool = ( y === out );
103+
// returns true
104+
105+
y = flattenBy.assign( x, [ 2, 2 ], true, out, 1, 0, scale );
106+
// returns <Float64Array>[ 2, 6, 4, 8 ]
107+
```
108+
109+
</section>
110+
111+
<!-- /.usage -->
112+
113+
<section class="notes">
114+
115+
## Notes
116+
117+
- A callback function is provided the following arguments:
118+
119+
- **value**: nested array element.
120+
- **indices**: element indices (in lexicographic order).
121+
- **arr**: the input array.
122+
123+
- Both functions assume that all nested arrays have the same length (i.e., the input array is **not** a ragged array).
124+
125+
</section>
126+
127+
<!-- /.notes -->
128+
129+
<section class="examples">
130+
131+
## Examples
132+
133+
<!-- eslint no-undef: "error" -->
134+
135+
```javascript
136+
var naryFunction = require( '@stdlib/utils/nary-function' );
137+
var abs = require( '@stdlib/math/base/special/abs' );
138+
var flattenBy = require( '@stdlib/array/base/flatten-by' );
139+
140+
var fcn = naryFunction( abs, 1 );
141+
142+
// Define a 2x2x1x2x2 array:
143+
var x = [
144+
[
145+
[
146+
[
147+
[ -1, -2 ], [ -3, -4 ]
148+
]
149+
],
150+
[
151+
[
152+
[ -5, -6 ], [ -7, -8 ]
153+
]
154+
]
155+
],
156+
[
157+
[
158+
[
159+
[ -9, -10 ], [ -11, -12 ]
160+
]
161+
],
162+
[
163+
[
164+
[ -13, -14 ], [ -15, -16 ]
165+
]
166+
]
167+
]
168+
];
169+
170+
var out = flattenBy( x, [ 0, 0, 0, 0, 0 ], false, fcn );
171+
// returns []
172+
173+
out = flattenBy( x, [ 0, 0, 0, 0, 0 ], true, fcn );
174+
// returns []
175+
176+
out = flattenBy( x, [ 1, 1, 1, 1, 1 ], false, fcn );
177+
// returns [ 1 ]
178+
179+
out = flattenBy( x, [ 1, 1, 1, 1, 1 ], true, fcn );
180+
// returns [ 1 ]
181+
182+
out = flattenBy( x, [ 1, 2, 1, 2, 2 ], false, fcn );
183+
// returns [ 1, 2, 3, 4, 5, 6, 7, 8 ]
184+
185+
out = flattenBy( x, [ 1, 2, 1, 2, 2 ], true, fcn );
186+
// returns [ 1, 5, 3, 7, 2, 6, 4, 8 ]
187+
188+
out = flattenBy( x, [ 2, 2, 1, 2, 2 ], false, fcn );
189+
// returns [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]
190+
191+
out = flattenBy( x, [ 2, 2, 1, 2, 2 ], true, fcn );
192+
// returns [ 1, 9, 5, 13, 3, 11, 7, 15, 2, 10, 6, 14, 4, 12, 8, 16 ]
193+
```
194+
195+
</section>
196+
197+
<!-- /.examples -->
198+
199+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
200+
201+
<section class="related">
202+
203+
</section>
204+
205+
<!-- /.related -->
206+
207+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
208+
209+
<section class="links">
210+
211+
</section>
212+
213+
<!-- /.links -->

0 commit comments

Comments
 (0)