Skip to content

Commit 61266ec

Browse files
Neerajpathak07kgryte
authored andcommitted
feat: add function/thunk
ref: #5445
1 parent 0025233 commit 61266ec

File tree

10 files changed

+720
-0
lines changed

10 files changed

+720
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
# thunk
22+
23+
> Generate a [thunk][thunk].
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 thunk = require( '@stdlib/function/thunk' );
41+
```
42+
43+
#### thunk( fcn\[, ...args] )
44+
45+
Returns a [thunk][thunk] (i.e., an anonymous function having arity `0` and which invokes a provided function with specified arguments).
46+
47+
```javascript
48+
var add = require( '@stdlib/number/float64/base/add' );
49+
50+
var f = thunk( add, 2.0, 3.0 );
51+
// returns <Function>
52+
53+
var v = f();
54+
// returns 5
55+
56+
// ...
57+
58+
v = f();
59+
// returns 5
60+
```
61+
62+
</section>
63+
64+
<!-- /.usage -->
65+
66+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
67+
68+
<section class="notes">
69+
70+
</section>
71+
72+
<!-- /.notes -->
73+
74+
<!-- Package usage examples. -->
75+
76+
<section class="examples">
77+
78+
## Examples
79+
80+
<!-- eslint no-undef: "error" -->
81+
82+
```javascript
83+
var add = require( '@stdlib/number/float64/base/add' );
84+
var decorateAfter = require( '@stdlib/utils/decorate-after' );
85+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
86+
var thunk = require( '@stdlib/function/thunk' );
87+
88+
function log( v ) {
89+
console.log( v );
90+
}
91+
92+
// Create a PRNG for generating uniformly distributed pseudorandom integers:
93+
var randi = discreteUniform( 100, 1000 );
94+
95+
// Randomly delay evaluation of various thunks...
96+
var i;
97+
for ( i = 0; i < 10; i++ ) {
98+
setTimeout( decorateAfter( thunk( add, i, i+1 ), 0, log ), randi() );
99+
}
100+
```
101+
102+
</section>
103+
104+
<!-- /.examples -->
105+
106+
<!-- 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. -->
107+
108+
<section class="references">
109+
110+
</section>
111+
112+
<!-- /.references -->
113+
114+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
115+
116+
<section class="related">
117+
118+
</section>
119+
120+
<!-- /.related -->
121+
122+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
123+
124+
<section class="links">
125+
126+
[thunk]: https://en.wikipedia.org/wiki/Thunk
127+
128+
<!-- <related-links> -->
129+
130+
<!-- </related-links> -->
131+
132+
</section>
133+
134+
<!-- /.links -->
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 isFunction = require( '@stdlib/assert/is-function' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var abs = require( '@stdlib/math/base/special/abs' );
27+
var pkg = require( './../package.json' ).name;
28+
var thunk = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var out;
35+
var i;
36+
37+
b.tic();
38+
for ( i = 0; i < b.iterations; i++ ) {
39+
out = thunk( abs, -0.0 );
40+
if ( typeof out !== 'function' ) {
41+
b.fail( 'should return a function' );
42+
}
43+
}
44+
b.toc();
45+
if ( !isFunction( out ) ) {
46+
b.fail( 'should return a function' );
47+
}
48+
b.pass( 'benchmark finished' );
49+
b.end();
50+
});
51+
52+
bench( pkg+'::thunk', function benchmark( b ) {
53+
var fcn;
54+
var out;
55+
var i;
56+
57+
fcn = thunk( abs, -0.0 );
58+
59+
b.tic();
60+
for ( i = 0; i < b.iterations; i++ ) {
61+
out = fcn(); // NOTE: this may get optimized away as returning the same value for each loop iteration
62+
if ( out !== out ) {
63+
b.fail( 'should not return NaN' );
64+
}
65+
}
66+
b.toc();
67+
if ( isnan( out ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
b.pass( 'benchmark finished' );
71+
b.end();
72+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
{{alias}}( fcn[, ...args] )
3+
Returns a thunk.
4+
5+
Parameters
6+
----------
7+
fcn: Function
8+
Function to convert to a thunk.
9+
10+
args: ...any (optional)
11+
Function arguments.
12+
13+
Returns
14+
-------
15+
out: Function
16+
Thunk.
17+
18+
Examples
19+
--------
20+
> var fcn = {{alias}}( {{alias:@stdlib/number/float64/base/add}}, 2, 3 );
21+
> var v = fcn()
22+
5
23+
> v = fcn()
24+
5
25+
26+
See Also
27+
--------
28+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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: 4.1
20+
21+
/**
22+
* Returns a thunk.
23+
*
24+
* @param fcn - function to convert to a thunk
25+
* @param args - function args
26+
* @returns thunk
27+
*
28+
* @example
29+
* var add = require( '@stdlib/number/float64/base/add' );
30+
*
31+
* var f = thunk( add, 2, 3 );
32+
* // returns <Function>
33+
*
34+
* // ...
35+
*
36+
* // Evaluate the thunk:
37+
* var v = f();
38+
* // returns 5
39+
*/
40+
declare function thunk<T extends Array<any>, U>( fcn: ( ...args: T ) => U, ...args: T ): () => U;
41+
42+
43+
// EXPORTS //
44+
45+
export = thunk;
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
import thunk = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns a function...
25+
{
26+
thunk<[], number>( (): number => 2 ); // $ExpectType () => number
27+
thunk<Array<number>, number>( ( x: number ): number => x, 2 ); // $ExpectType () => number
28+
thunk<[number, number], number>( ( x: number, y: number ): number => x + y, 2, 3 ); // $ExpectType () => number
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not a function...
32+
{
33+
thunk( true ); // $ExpectError
34+
thunk( false ); // $ExpectError
35+
thunk( 5 ); // $ExpectError
36+
thunk( [] ); // $ExpectError
37+
thunk( {} ); // $ExpectError
38+
thunk( 'abc' ); // $ExpectError
39+
40+
thunk( true, 2 ); // $ExpectError
41+
thunk( false, 2 ); // $ExpectError
42+
thunk( 5, 2 ); // $ExpectError
43+
thunk( [], 2 ); // $ExpectError
44+
thunk( {}, 2 ); // $ExpectError
45+
thunk( 'abc', 2 ); // $ExpectError
46+
47+
thunk( true, 2, 3 ); // $ExpectError
48+
thunk( false, 2, 3 ); // $ExpectError
49+
thunk( 5, 2, 3 ); // $ExpectError
50+
thunk( [], 2, 3 ); // $ExpectError
51+
thunk( {}, 2, 3 ); // $ExpectError
52+
thunk( 'abc', 2, 3 ); // $ExpectError
53+
}
54+
55+
// The compiler throws an error if the function is provided an incorrect number of arguments...
56+
{
57+
thunk(); // $ExpectError
58+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
var add = require( '@stdlib/number/float64/base/add' );
22+
var decorateAfter = require( '@stdlib/utils/decorate-after' );
23+
var discreteUniform = require( '@stdlib/random/base/discrete-uniform' ).factory;
24+
var thunk = require( './../lib' );
25+
26+
function log( v ) {
27+
console.log( v );
28+
}
29+
30+
// Create a PRNG for generating uniformly distributed pseudorandom integers:
31+
var randi = discreteUniform( 100, 1000 );
32+
33+
// Randomly delay evaluation of various thunks...
34+
var i;
35+
for ( i = 0; i < 10; i++ ) {
36+
setTimeout( decorateAfter( thunk( add, i, i+1 ), 0, log ), randi() );
37+
}

0 commit comments

Comments
 (0)