Skip to content

Commit 0499e82

Browse files
committed
Add utility to apply specified arguments to a provided function
1 parent f2a4c7d commit 0499e82

File tree

10 files changed

+982
-0
lines changed

10 files changed

+982
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2021 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+
# pickArguments
22+
23+
> Create a function that invokes a provided function with specified arguments.
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 pickArguments = require( '@stdlib/utils/pick-arguments' );
41+
```
42+
43+
#### pickArguments( fcn, indices\[, thisArg] )
44+
45+
Returns a `function` that invokes a provided function with specified arguments.
46+
47+
```javascript
48+
function foo( a, b ) {
49+
return [ a, b ];
50+
}
51+
52+
var bar = pickArguments( foo, [ 0, 2 ] );
53+
54+
var out = bar( 1, 2, 3 );
55+
// returns [ 1, 3 ]
56+
```
57+
58+
To set the `this` context when invoking the provided function, provide a `thisArg`.
59+
60+
<!-- eslint-disable no-restricted-syntax -->
61+
62+
```javascript
63+
function Foo() {
64+
this.x = 1;
65+
this.y = 2;
66+
}
67+
68+
Foo.prototype.scale = function scale( a, b ) {
69+
return [ this.x*a, this.y*b ];
70+
};
71+
72+
var ctx = {
73+
'x': 10,
74+
'y': 20
75+
};
76+
77+
var foo = new Foo();
78+
79+
var bar = pickArguments( foo.scale, [ 0, 2 ], ctx );
80+
81+
var out = bar( 1, 2, 3 );
82+
// returns [ 10, 60 ]
83+
```
84+
85+
</section>
86+
87+
<!-- /.usage -->
88+
89+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
90+
91+
<section class="notes">
92+
93+
</section>
94+
95+
<!-- /.notes -->
96+
97+
<!-- Package usage examples. -->
98+
99+
<section class="examples">
100+
101+
## Examples
102+
103+
<!-- eslint no-undef: "error" -->
104+
105+
```javascript
106+
var filledarrayBy = require( '@stdlib/array/filled-by' );
107+
var add = require( '@stdlib/math/base/ops/add' );
108+
var pickArguments = require( '@stdlib/utils/pick-arguments' );
109+
110+
function fill( i ) {
111+
return i;
112+
}
113+
114+
// Create a data array:
115+
var x = filledarrayBy( 10, 'float64', fill );
116+
117+
// Compute the sum of consecutive elements...
118+
var f;
119+
var i;
120+
for ( i = 1; i < x.length; i++ ) {
121+
f = pickArguments( add, [ i-1, i ] );
122+
console.log( 'sum(x_%d, x_%d) = %d', i-1, i, f.apply( null, x ) );
123+
}
124+
```
125+
126+
</section>
127+
128+
<!-- /.examples -->
129+
130+
<!-- 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. -->
131+
132+
<section class="references">
133+
134+
</section>
135+
136+
<!-- /.references -->
137+
138+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
139+
140+
<section class="related">
141+
142+
</section>
143+
144+
<!-- /.related -->
145+
146+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
147+
148+
<section class="links">
149+
150+
</section>
151+
152+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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 isFunction = require( '@stdlib/assert/is-function' );
25+
var isArray = require( '@stdlib/assert/is-array' );
26+
var pkg = require( './../package.json' ).name;
27+
var pickArguments = require( './../lib' );
28+
29+
30+
// FUNCTIONS //
31+
32+
function foo( a, b ) {
33+
return [ a, b ];
34+
}
35+
36+
37+
// MAIN //
38+
39+
bench( pkg, function benchmark( b ) {
40+
var out;
41+
var idx;
42+
var i;
43+
44+
idx = [ 0, 2 ];
45+
46+
b.tic();
47+
for ( i = 0; i < b.iterations; i++ ) {
48+
out = pickArguments( foo, idx );
49+
if ( typeof out !== 'function' ) {
50+
b.fail( 'should return a function' );
51+
}
52+
}
53+
b.toc();
54+
if ( !isFunction( out ) ) {
55+
b.fail( 'should return a function' );
56+
}
57+
b.pass( 'benchmark finished' );
58+
b.end();
59+
});
60+
61+
bench( pkg+'::returned_function', function benchmark( b ) {
62+
var bar;
63+
var out;
64+
var i;
65+
66+
bar = pickArguments( foo, [ 0, 2 ] );
67+
b.tic();
68+
for ( i = 0; i < b.iterations; i++ ) {
69+
out = bar( i, i+1, i+2 );
70+
if ( typeof out !== 'object' ) {
71+
b.fail( 'should return an array' );
72+
}
73+
}
74+
b.toc();
75+
if ( !isArray( out ) ) {
76+
b.fail( 'should return an array' );
77+
}
78+
b.pass( 'benchmark finished' );
79+
b.end();
80+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
{{alias}}( fcn, indices[, thisArg] )
3+
Returns a function that applies specified arguments to a provided function.
4+
5+
Parameters
6+
----------
7+
fcn: Function
8+
Input function.
9+
10+
indices: Array<integer>
11+
Argument indices.
12+
13+
thisArg: any (optional)
14+
Function context.
15+
16+
Returns
17+
-------
18+
out: Function
19+
Function wrapper.
20+
21+
Examples
22+
--------
23+
> function foo( a, b ) { return [ a, b ]; };
24+
> var bar = {{alias}}( foo, [ 0, 2 ] );
25+
> var out = bar( 1, 2, 3 )
26+
[ 1, 3 ]
27+
28+
See Also
29+
--------
30+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
/// <reference types="@stdlib/types"/>
22+
23+
import { ArrayLike } from '@stdlib/types/array';
24+
25+
/**
26+
* Returns a function that applies specified arguments to a provided function.
27+
*
28+
* @param fcn - input function
29+
* @param indices - argument indices
30+
* @param thisArg - function context
31+
* @returns function wrapper
32+
*
33+
* @example
34+
* function foo( a, b ) {
35+
* return [ a, b ];
36+
* }
37+
*
38+
* var bar = pickArguments( foo, [ 0, 2 ] );
39+
*
40+
* var out = bar( 1, 2, 3 );
41+
* // returns [ 1, 3 ]
42+
*/
43+
declare function pickArguments( fcn: Function, indices: ArrayLike<number>, thisArg?: any ): Function; // tslint-disable-line max-line-length
44+
45+
46+
// EXPORTS //
47+
48+
export = pickArguments;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2021 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+
import pickArguments = require( './index' );
20+
21+
/**
22+
* Input function.
23+
*
24+
* @param x - input value
25+
* @param y - input value
26+
* @param z - input value
27+
* @returns array of input values
28+
*/
29+
function foo( x: any, y: any, z: any ): Array<any> {
30+
return [ x, y, z ];
31+
}
32+
33+
34+
// TESTS //
35+
36+
// The function returns a function...
37+
{
38+
pickArguments( foo, [ 0, 2 ] ); // $ExpectType Function
39+
pickArguments( foo, [ 0, 2 ], {} ); // $ExpectType Function
40+
}
41+
42+
// The compiler throws an error if the function is provided a first argument other than a function...
43+
{
44+
pickArguments( true, [ 0, 2 ] ); // $ExpectError
45+
pickArguments( false, [ 0, 2 ] ); // $ExpectError
46+
pickArguments( 5, [ 0, 2 ] ); // $ExpectError
47+
pickArguments( [], [ 0, 2 ] ); // $ExpectError
48+
pickArguments( {}, [ 0, 2 ] ); // $ExpectError
49+
pickArguments( 'abc', [ 0, 2 ] ); // $ExpectError
50+
}
51+
52+
// The compiler throws an error if the function is provided a second argument other than an array-like object containing numbers...
53+
{
54+
pickArguments( foo, true ); // $ExpectError
55+
pickArguments( foo, false ); // $ExpectError
56+
pickArguments( foo, 123 ); // $ExpectError
57+
pickArguments( foo, {} ); // $ExpectError
58+
pickArguments( foo, [ '1' ] ); // $ExpectError
59+
}
60+
61+
// The compiler throws an error if the function is provided more than three arguments...
62+
{
63+
pickArguments( foo, [ 0, 2 ], {}, 4 ); // $ExpectError
64+
}

0 commit comments

Comments
 (0)