Skip to content

Commit 91c75b7

Browse files
committed
Add array utility to create a filled generic array according to a callback function
1 parent 0d716e8 commit 91c75b7

File tree

10 files changed

+720
-0
lines changed

10 files changed

+720
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2022 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+
# filledBy
22+
23+
> Create a filled "generic" 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 filledBy = require( '@stdlib/array/base/filled-by' );
41+
```
42+
43+
#### filledBy( len, clbk\[, thisArg] )
44+
45+
Returns a filled "generic" array according to a provided callback function.
46+
47+
```javascript
48+
function clbk( i ) {
49+
return i;
50+
}
51+
52+
var out = filledBy( 3, clbk );
53+
// returns [ 0, 1, 2 ]
54+
```
55+
56+
When invoked, a callback function is provided a single argument:
57+
58+
- **index**: the current array index.
59+
60+
To set the callback execution context, provide a `thisArg`.
61+
62+
<!-- eslint-disable no-invalid-this -->
63+
64+
```javascript
65+
function clbk( i ) {
66+
this.count += 1;
67+
return i;
68+
}
69+
70+
var ctx = {
71+
'count': 0
72+
};
73+
74+
var out = filledBy( 3, clbk, ctx );
75+
// returns [ 0, 1, 2 ];
76+
77+
var cnt = ctx.count;
78+
// returns 3
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 filledBy = require( '@stdlib/array/base/filled-by' );
104+
105+
var out = filledBy( 3, constantFunction( 0.0 ) );
106+
// returns [ 0.0, 0.0, 0.0 ]
107+
108+
out = filledBy( 3, constantFunction( 'beep' ) );
109+
// returns [ 'beep', 'beep', 'beep' ]
110+
111+
out = filledBy( 3, constantFunction( null ) );
112+
// returns [ null, null, null ]
113+
114+
out = filledBy( 3, constantFunction( true ) );
115+
// returns [ true, true, true ]
116+
117+
out = filledBy( 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 -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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 isArray = require( '@stdlib/assert/is-array' );
26+
var constantFunction = require( '@stdlib/utils/constant-function' );
27+
var pkg = require( './../package.json' ).name;
28+
var filledBy = require( './../lib' );
29+
30+
31+
// FUNCTIONS //
32+
33+
/**
34+
* Creates a benchmark function.
35+
*
36+
* @private
37+
* @param {PositiveInteger} len - array length
38+
* @returns {Function} benchmark function
39+
*/
40+
function createBenchmark( len ) {
41+
return benchmark;
42+
43+
/**
44+
* Benchmark function.
45+
*
46+
* @private
47+
* @param {Benchmark} b - benchmark instance
48+
*/
49+
function benchmark( b ) {
50+
var out;
51+
var f;
52+
var i;
53+
54+
f = constantFunction( len );
55+
56+
b.tic();
57+
for ( i = 0; i < b.iterations; i++ ) {
58+
out = filledBy( len, f );
59+
if ( out.length !== len ) {
60+
b.fail( 'unexpected length' );
61+
}
62+
}
63+
b.toc();
64+
if ( !isArray( out ) ) {
65+
b.fail( 'should return an array' );
66+
}
67+
b.pass( 'benchmark finished' );
68+
b.end();
69+
}
70+
}
71+
72+
73+
// MAIN //
74+
75+
/**
76+
* Main execution sequence.
77+
*
78+
* @private
79+
*/
80+
function main() {
81+
var len;
82+
var min;
83+
var max;
84+
var f;
85+
var i;
86+
87+
min = 1; // 10^min
88+
max = 6; // 10^max
89+
90+
for ( i = min; i <= max; i++ ) {
91+
len = pow( 10, i );
92+
93+
f = createBenchmark( len );
94+
bench( pkg+':len='+len, f );
95+
}
96+
}
97+
98+
main();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
{{alias}}( len, clbk[, thisArg] )
3+
Returns a filled "generic" array according to a provided callback function.
4+
5+
Parameters
6+
----------
7+
len: integer
8+
Array length.
9+
10+
clbk: Function
11+
Callback function.
12+
13+
thisArg: any (optional)
14+
Callback execution context.
15+
16+
Returns
17+
-------
18+
out: Array
19+
Output array.
20+
21+
Examples
22+
--------
23+
> function clbk() { return 1.0; };
24+
> var out = {{alias}}( 3, clbk )
25+
[ 1.0, 1.0, 1.0 ]
26+
27+
See Also
28+
--------
29+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2022 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: 2.0
20+
21+
/**
22+
* Nullary callback function.
23+
*
24+
* @returns fill value
25+
*/
26+
type Nullary = () => any;
27+
28+
/**
29+
* Unary callback function.
30+
*
31+
* @param i - current array index
32+
* @returns fill value
33+
*/
34+
type Unary = ( i: number ) => any;
35+
36+
/**
37+
* Callback function.
38+
*
39+
* @param i - current array index
40+
* @returns fill value
41+
*/
42+
type Callback = Nullary | Unary;
43+
44+
/**
45+
* Returns a filled "generic" array according to a provided callback function.
46+
*
47+
* @param len - array length
48+
* @param clbk - callback function
49+
* @param thisArg - callback function execution context
50+
* @returns output array
51+
*
52+
* @example
53+
* var constantFunction = require( `@stdlib/utils/constant-function` );
54+
*
55+
* var arr = filledBy( 5, constantFunction( 1.0 ) );
56+
* // returns [ 1.0, 1.0, 1.0, 1.0, 1.0 ]
57+
*/
58+
declare function filledBy( len: number, clbk: Callback, thisArg?: any ): Array<any>; // tslint:disable-line:max-line-length
59+
60+
61+
// EXPORTS //
62+
63+
export = filledBy;

0 commit comments

Comments
 (0)