@ohos-ports/stdlib-function 0.0.0-stage → 0.4.1-beta.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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 -->
@@ -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
+
@@ -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;
@@ -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
+ }
@@ -0,0 +1,47 @@
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
+ /**
22
+ * Generate a thunk.
23
+ *
24
+ * @module @stdlib/function/thunk
25
+ *
26
+ * @example
27
+ * var add = require( '@ohos-ports/stdlib-number/float64/base/add' );
28
+ * var thunk = require( '@ohos-ports/stdlib-function/thunk' );
29
+ *
30
+ * var f = thunk( add, 2, 3 );
31
+ * // returns <Function>
32
+ *
33
+ * // ...
34
+ *
35
+ * // Evaluate the thunk:
36
+ * var v = f();
37
+ * // returns 5
38
+ */
39
+
40
+ // MODULES //
41
+
42
+ var main = require( './main.js' );
43
+
44
+
45
+ // EXPORTS //
46
+
47
+ module.exports = main;
@@ -0,0 +1,68 @@
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 isFunction = require( '@ohos-ports/stdlib-assert/is-function' );
24
+ var format = require( '@ohos-ports/stdlib-string/format' );
25
+
26
+
27
+ // MAIN //
28
+
29
+ /**
30
+ * Returns a thunk.
31
+ *
32
+ * @param {Function} fcn - function to convert to a thunk
33
+ * @param {...*} [args] - function arguments
34
+ * @throws {TypeError} first argument must be a function
35
+ * @returns {Function} thunk
36
+ *
37
+ * @example
38
+ * var add = require( '@ohos-ports/stdlib-number/float64/base/add' );
39
+ *
40
+ * var f = thunk( add, 2, 3 );
41
+ * // returns <Function>
42
+ *
43
+ * // ...
44
+ *
45
+ * // Evaluate the thunk:
46
+ * var v = f();
47
+ * // returns 5
48
+ */
49
+ function thunk( fcn ) {
50
+ var args;
51
+ var i;
52
+ if ( !isFunction( fcn ) ) {
53
+ throw new TypeError( format( 'invalid argument. First argument must be a function. Value: `%s`.', fcn ) );
54
+ }
55
+ args = [];
56
+ for ( i = 1; i < arguments.length; i++ ) {
57
+ args.push( arguments[ i ] );
58
+ }
59
+ // NOTE: we return an anonymous function to satisfy the definition of a thunk (i.e., an anonymous function which has no parameters of its own and wraps another function)...
60
+ return function () { // eslint-disable-line no-restricted-syntax, func-names
61
+ return fcn.apply( null, args );
62
+ };
63
+ }
64
+
65
+
66
+ // EXPORTS //
67
+
68
+ module.exports = thunk;
@@ -0,0 +1,68 @@
1
+ {
2
+ "name": "@stdlib/function/thunk",
3
+ "version": "0.0.0",
4
+ "description": "Generate a thunk.",
5
+ "license": "Apache-2.0",
6
+ "author": {
7
+ "name": "The Stdlib Authors",
8
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
9
+ },
10
+ "contributors": [
11
+ {
12
+ "name": "The Stdlib Authors",
13
+ "url": "https://github.com/stdlib-js/stdlib/graphs/contributors"
14
+ }
15
+ ],
16
+ "main": "./lib",
17
+ "directories": {
18
+ "benchmark": "./benchmark",
19
+ "doc": "./docs",
20
+ "example": "./examples",
21
+ "lib": "./lib",
22
+ "test": "./test"
23
+ },
24
+ "types": "./docs/types",
25
+ "scripts": {},
26
+ "homepage": "https://github.com/stdlib-js/stdlib",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git://github.com/stdlib-js/stdlib.git"
30
+ },
31
+ "bugs": {
32
+ "url": "https://github.com/stdlib-js/stdlib/issues"
33
+ },
34
+ "dependencies": {},
35
+ "devDependencies": {},
36
+ "engines": {
37
+ "node": ">=0.10.0",
38
+ "npm": ">2.7.0"
39
+ },
40
+ "os": [
41
+ "aix",
42
+ "darwin",
43
+ "freebsd",
44
+ "linux",
45
+ "macos",
46
+ "openbsd",
47
+ "sunos",
48
+ "win32",
49
+ "windows"
50
+ ],
51
+ "keywords": [
52
+ "stdlib",
53
+ "stdutils",
54
+ "stdutil",
55
+ "utilities",
56
+ "utils",
57
+ "util",
58
+ "thunk",
59
+ "delay",
60
+ "wrap",
61
+ "lazy",
62
+ "function",
63
+ "fun",
64
+ "func",
65
+ "fcn",
66
+ "functional"
67
+ ]
68
+ }
@@ -0,0 +1,153 @@
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
+ # function2string
22
+
23
+ > Return a string representing the source code of a provided 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 function2string = require( '@stdlib/function/to-string' );
41
+ ```
42
+
43
+ #### function2string( fcn )
44
+
45
+ Returns a string representing the source code of a provided function.
46
+
47
+ ```javascript
48
+ function add( x, y ) {
49
+ return x + y;
50
+ }
51
+
52
+ var str = function2string( add );
53
+ // e.g., returns 'function add( x, y ) {\n return x + y;\n}'
54
+ ```
55
+
56
+ </section>
57
+
58
+ <!-- /.usage -->
59
+
60
+ <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
61
+
62
+ <section class="notes">
63
+
64
+ ## Notes
65
+
66
+ - If called on built-in functions, functions created by `Function.prototype.bind()`, or other non-JavaScript functions, the function returns a "native" function string similar to the following:
67
+
68
+ ```text
69
+ "function foo() { [native code] }"
70
+ ```
71
+
72
+ For intrinsic object methods and functions, `foo` is the initial name of the function.
73
+
74
+ - If called on a function created by the [`Function`][@stdlib/function/ctor] constructor, the function returns the source code of a synthesized function declaration having the name "anonymous" and using the provided parameters and function body.
75
+
76
+ - Starting in ES2018, the ECMAScript specification requires that the returned string contain the exact same source code as it was declared, including any whitespace and/or comments. If the host is unable to access the source code, the specification requires that the returned string be the native function string.
77
+
78
+ </section>
79
+
80
+ <!-- /.notes -->
81
+
82
+ <!-- Package usage examples. -->
83
+
84
+ <section class="examples">
85
+
86
+ ## Examples
87
+
88
+ <!-- eslint no-undef: "error" -->
89
+
90
+ ```javascript
91
+ var ndarray = require( '@stdlib/ndarray/ctor' );
92
+ var erf = require( '@stdlib/math/base/special/erf' );
93
+ var daxpy = require( '@stdlib/blas/base/daxpy' );
94
+ var kebabcase = require( '@stdlib/string/kebabcase' );
95
+ var randu = require( '@stdlib/random/base/randu' );
96
+ var format = require( '@stdlib/string/format' );
97
+ var function2string = require( '@stdlib/function/to-string' );
98
+
99
+ var fcns = [
100
+ [ 'ndarray', ndarray ],
101
+ [ 'erf', erf ],
102
+ [ 'daxpy', daxpy ],
103
+ [ 'kebabcase', kebabcase ],
104
+ [ 'randu', randu ],
105
+ [ 'format', format ],
106
+ [ 'function2string', function2string ]
107
+ ];
108
+
109
+ var i;
110
+ for ( i = 0; i < fcns.length; i++ ) {
111
+ console.log( format( 'Function: %s\n\n%s\n', fcns[ i ][ 0 ], function2string( fcns[ i ][ 1 ] ) ) );
112
+ }
113
+ ```
114
+
115
+ </section>
116
+
117
+ <!-- /.examples -->
118
+
119
+ <!-- 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. -->
120
+
121
+ <section class="references">
122
+
123
+ </section>
124
+
125
+ <!-- /.references -->
126
+
127
+ <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
128
+
129
+ <section class="related">
130
+
131
+ * * *
132
+
133
+ ## See Also
134
+
135
+ - <span class="package-name">[`@stdlib/function/ctor`][@stdlib/function/ctor]</span><span class="delimiter">: </span><span class="description">function constructor.</span>
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
+ [@stdlib/function/ctor]: https://github.com/stdlib-js/function/tree/main/ctor
146
+
147
+ <!-- <related-links> -->
148
+
149
+ <!-- </related-links> -->
150
+
151
+ </section>
152
+
153
+ <!-- /.links -->
@@ -0,0 +1,41 @@
1
+
2
+ {{alias}}( fcn )
3
+ Returns a string representing the source code of a provided function.
4
+
5
+ If called on built-in functions, functions created by `Function#bind()`, or
6
+ other non-JavaScript functions, the function returns a "native" function
7
+ string similar to the following:
8
+
9
+ "function foo() { [native code] }"
10
+
11
+ For intrinsic object methods and functions, `foo` is the initial name of the
12
+ function.
13
+
14
+ If called on a function created by the `Function` constructor, the function
15
+ returns the source code of a synthesized function declaration having the
16
+ name "anonymous" and using the provided parameters and function body.
17
+
18
+ Starting in ES2018, the ECMAScript specification requires that the returned
19
+ string contain the exact same source code as it was declared, including any
20
+ whitespace and/or comments. If the host is unable to access the source code,
21
+ the specification requires that the returned string be the native function
22
+ string.
23
+
24
+ Parameters
25
+ ----------
26
+ fcn: Function
27
+ Input function.
28
+
29
+ Returns
30
+ -------
31
+ out: string
32
+ String representing the function's source code.
33
+
34
+ Examples
35
+ --------
36
+ > {{alias}}( {{alias:@stdlib/math/base/special/erf}} )
37
+ <string>
38
+
39
+ See Also
40
+ --------
41
+
@@ -0,0 +1,54 @@
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 string representing the source code of a provided function.
23
+ *
24
+ * ## Notes
25
+ *
26
+ * - If called on built-in functions, functions created by `Function.prototype.bind()`, or other non-JavaScript functions, the function returns a "native" function string similar to the following:
27
+ *
28
+ * ```text
29
+ * "function foo() { [native code] }"
30
+ * ```
31
+ *
32
+ * For intrinsic object methods and functions, `foo` is the initial name of the function.
33
+ *
34
+ * - If called on a function created by the `Function` constructor, the function returns the source code of a synthesized function declaration having the name "anonymous" and using the provided parameters and function body.
35
+ *
36
+ * - Starting in ES2018, the ECMAScript specification requires that the returned string contain the exact same source code as it was declared, including any whitespace and/or comments. If the host is unable to access the source code, the specification requires that the returned string be the native function string.
37
+ *
38
+ * @param fcn - input function
39
+ * @returns string representing function source code
40
+ *
41
+ * @example
42
+ * function add( x, y ) {
43
+ * return x + y;
44
+ * }
45
+ *
46
+ * var str = function2string( add );
47
+ * // e.g., returns 'function add( x, y ) {\n return x + y;\n}'
48
+ */
49
+ declare function function2string( fcn: Function ): string;
50
+
51
+
52
+ // EXPORTS //
53
+
54
+ export = function2string;