Skip to content

Commit 572905f

Browse files
committed
feat: add array/base/filled4d-by
1 parent 49c3bcd commit 572905f

File tree

10 files changed

+943
-0
lines changed

10 files changed

+943
-0
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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+
# filled4dBy
22+
23+
> Create a filled four-dimensional nested array according to a provided callback function.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var filled4dBy = require( '@stdlib/array/base/filled4d-by' );
41+
```
42+
43+
#### filled4dBy( len, clbk\[, thisArg] )
44+
45+
Returns a filled four-dimensional nested array according to a provided callback function.
46+
47+
```javascript
48+
function clbk() {
49+
return 1.0;
50+
}
51+
52+
var out = filled4dBy( [ 1, 2, 1, 3 ], clbk );
53+
// returns [ [ [ [ 1.0, 1.0, 1.0 ] ], [ [ 1.0, 1.0, 1.0 ] ] ] ]
54+
```
55+
56+
When invoked, a callback function is provided a single argument:
57+
58+
- **indices**: current array element indices.
59+
60+
To set the callback execution context, provide a `thisArg`.
61+
62+
<!-- eslint-disable no-invalid-this -->
63+
64+
```javascript
65+
function clbk() {
66+
this.count += 1;
67+
return 1.0;
68+
}
69+
70+
var ctx = {
71+
'count': 0
72+
};
73+
74+
var out = filled4dBy( [ 1, 1, 2, 3 ], clbk, ctx );
75+
// returns [ [ [ [ 1.0, 1.0, 1.0 ], [ 1.0, 1.0, 1.0 ] ] ] ];
76+
77+
var cnt = ctx.count;
78+
// returns 6
79+
```
80+
81+
</section>
82+
83+
<!-- /.usage -->
84+
85+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
86+
87+
<section class="notes">
88+
89+
</section>
90+
91+
<!-- /.notes -->
92+
93+
<!-- Package usage examples. -->
94+
95+
<section class="examples">
96+
97+
## Examples
98+
99+
<!-- eslint no-undef: "error" -->
100+
101+
```javascript
102+
var constantFunction = require( '@stdlib/utils/constant-function' );
103+
var filled4dBy = require( '@stdlib/array/base/filled4d-by' );
104+
105+
var out = filled4dBy( [ 1, 1, 1, 3 ], constantFunction( 0.0 ) );
106+
// returns [ [ [ [ 0.0, 0.0, 0.0 ] ] ] ]
107+
108+
out = filled4dBy( [ 1, 1, 3, 1 ], constantFunction( 'beep' ) );
109+
// returns [ [ [ [ 'beep' ], [ 'beep' ], [ 'beep' ] ] ] ]
110+
111+
out = filled4dBy( [ 1, 1, 1, 3 ], constantFunction( null ) );
112+
// returns [ [ [ [ null, null, null ] ] ] ]
113+
114+
out = filled4dBy( [ 1, 1, 3, 1 ], constantFunction( true ) );
115+
// returns [ [ [ [ true ], [ true ], [ true ] ] ] ]
116+
117+
out = filled4dBy( [ 1, 1, 1, 3 ], constantFunction( void 0 ) );
118+
// returns [ [ [ [ undefined, undefined, undefined ] ] ] ]
119+
```
120+
121+
</section>
122+
123+
<!-- /.examples -->
124+
125+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
126+
127+
<section class="references">
128+
129+
</section>
130+
131+
<!-- /.references -->
132+
133+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
134+
135+
<section class="related">
136+
137+
</section>
138+
139+
<!-- /.related -->
140+
141+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
142+
143+
<section class="links">
144+
145+
</section>
146+
147+
<!-- /.links -->
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
'use strict';
20+
21+
// MODULES //
22+
23+
var bench = require( '@stdlib/bench' );
24+
var pow = require( '@stdlib/math/base/special/pow' );
25+
var floor = require( '@stdlib/math/base/special/floor' );
26+
var isArrayArray = require( '@stdlib/assert/is-array-array' );
27+
var constantFunction = require( '@stdlib/utils/constant-function' );
28+
var pkg = require( './../package.json' ).name;
29+
var filled4dBy = require( './../lib' );
30+
31+
32+
// FUNCTIONS //
33+
34+
/**
35+
* Creates a benchmark function.
36+
*
37+
* @private
38+
* @param {PositiveInteger} N - array lengths
39+
* @returns {Function} benchmark function
40+
*/
41+
function createBenchmark( N ) {
42+
return benchmark;
43+
44+
/**
45+
* Benchmark function.
46+
*
47+
* @private
48+
* @param {Benchmark} b - benchmark instance
49+
*/
50+
function benchmark( b ) {
51+
var out;
52+
var f;
53+
var i;
54+
55+
f = constantFunction( N );
56+
57+
b.tic();
58+
for ( i = 0; i < b.iterations; i++ ) {
59+
out = filled4dBy( [ N, N, N, N ], f );
60+
if ( typeof out !== 'object' ) {
61+
b.fail( 'should return an array of arrays' );
62+
}
63+
}
64+
b.toc();
65+
if ( !isArrayArray( out ) ) {
66+
b.fail( 'should return an array of arrays' );
67+
}
68+
b.pass( 'benchmark finished' );
69+
b.end();
70+
}
71+
}
72+
73+
74+
// MAIN //
75+
76+
/**
77+
* Main execution sequence.
78+
*
79+
* @private
80+
*/
81+
function main() {
82+
var min;
83+
var max;
84+
var N;
85+
var f;
86+
var i;
87+
88+
min = 1; // 10^min
89+
max = 6; // 10^max
90+
91+
for ( i = min; i <= max; i++ ) {
92+
N = floor( pow( pow( 10, i ), 1.0/4.0 ) );
93+
94+
f = createBenchmark( N );
95+
bench( pkg+'::equidimensional:size='+(N*N*N*N), f );
96+
}
97+
}
98+
99+
main();
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
{{alias}}( shape, clbk[, thisArg] )
3+
Returns a filled four-dimensional nested array according to a provided
4+
callback function.
5+
6+
The callback function is provided one argument:
7+
8+
- indices: current array element indices.
9+
10+
Parameters
11+
----------
12+
shape: Array<integer>
13+
Array shape.
14+
15+
clbk: Function
16+
Callback function.
17+
18+
thisArg: any (optional)
19+
Callback execution context.
20+
21+
Returns
22+
-------
23+
out: Array
24+
Output array.
25+
26+
Examples
27+
--------
28+
> function clbk() { return 1.0; };
29+
> var out = {{alias}}( [ 1, 1, 1, 3 ], clbk )
30+
[ [ [ [ 1.0, 1.0, 1.0 ] ] ] ]
31+
32+
See Also
33+
--------
34+
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2023 The Stdlib Authors.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
// TypeScript Version: 4.1
20+
21+
/// <reference types="@stdlib/types"/>
22+
23+
import { Collection } from '@stdlib/types/object';
24+
25+
/**
26+
* Nullary callback function.
27+
*
28+
* @returns fill value
29+
*/
30+
type Nullary<T> = () => T;
31+
32+
/**
33+
* Unary callback function.
34+
*
35+
* @param indices - current array element indices
36+
* @returns fill value
37+
*/
38+
type Unary<T> = ( indices: Array<number> ) => T;
39+
40+
/**
41+
* Callback function.
42+
*
43+
* @param indices - current array element indices
44+
* @returns fill value
45+
*/
46+
type Callback<T> = Nullary<T> | Unary<T>;
47+
48+
/**
49+
* Returns a filled four-dimensional nested array according to a provided callback function.
50+
*
51+
* @param shape - array shape
52+
* @param clbk - callback function
53+
* @param thisArg - callback function execution context
54+
* @returns output array
55+
*
56+
* @example
57+
* var constantFunction = require( `@stdlib/utils/constant-function` );
58+
*
59+
* var arr = filled4dBy( [ 1, 1, 1, 5 ], constantFunction( 1.0 ) );
60+
* // returns [ [ [ [ 1.0, 1.0, 1.0, 1.0, 1.0 ] ] ] ]
61+
*/
62+
declare function filled4dBy<T = unknown>( shape: Collection<number>, clbk: Callback<T>, thisArg?: ThisParameterType<Callback<T>> ): Array<Array<Array<Array<T>>>>;
63+
64+
65+
// EXPORTS //
66+
67+
export = filled4dBy;

0 commit comments

Comments
 (0)