Skip to content

Commit 05f83ab

Browse files
ShraddheyaSkgryte
andauthored
Add iterator support for generating the sequence of fourth powers (#350)
* add iterator support for generating the sequence of fourth powers * Fix description Co-authored-by: Athan <kgryte@gmail.com>
1 parent faf9d8c commit 05f83ab

File tree

12 files changed

+1121
-0
lines changed

12 files changed

+1121
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2020 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+
# iterFourthPowersSeq
22+
23+
> Create an iterator which generates a sequence of [fourth powers][oeis-a000583].
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 iterFourthPowersSeq = require( '@stdlib/math/iter/sequences/fourth-powers' );
41+
```
42+
43+
#### iterFourthPowersSeq( \[options] )
44+
45+
Returns an iterator which generates a sequence of fourth powers.
46+
47+
```javascript
48+
var it = iterFourthPowersSeq();
49+
// returns <Object>
50+
51+
var v = it.next().value;
52+
// returns 0
53+
54+
v = it.next().value;
55+
// returns 1
56+
57+
v = it.next().value;
58+
// returns 16
59+
60+
v = it.next().value;
61+
// returns 81
62+
63+
v = it.next().value;
64+
// returns 256
65+
66+
// ...
67+
```
68+
69+
The returned iterator protocol-compliant object has the following properties:
70+
71+
- **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished.
72+
- **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
73+
74+
The function supports the following `options`:
75+
76+
- **iter**: number of iterations. Default: `9741`.
77+
78+
By default, the function returns a finite iterator to avoid exceeding the maximum safe double-precision floating-point integer. To adjust the number of iterations, set the `iter` option.
79+
80+
```javascript
81+
var opts = {
82+
'iter': 2
83+
};
84+
var it = iterFourthPowersSeq( opts );
85+
// returns <Object>
86+
87+
var v = it.next().value;
88+
// returns 0
89+
90+
v = it.next().value;
91+
// returns 1
92+
93+
var bool = it.next().done;
94+
// returns true
95+
```
96+
97+
</section>
98+
99+
<!-- /.usage -->
100+
101+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
102+
103+
<section class="notes">
104+
105+
## Notes
106+
107+
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
108+
109+
</section>
110+
111+
<!-- /.notes -->
112+
113+
<!-- Package usage examples. -->
114+
115+
<section class="examples">
116+
117+
## Examples
118+
119+
<!-- eslint no-undef: "error" -->
120+
121+
```javascript
122+
var iterFourthPowersSeq = require( '@stdlib/math/iter/sequences/fourth-powers' );
123+
124+
// Create an iterator:
125+
var opts = {
126+
'iter': 100
127+
};
128+
var it = iterFourthPowersSeq( opts );
129+
130+
// Perform manual iteration...
131+
var v;
132+
while ( true ) {
133+
v = it.next();
134+
if ( v.done ) {
135+
break;
136+
}
137+
console.log( v.value );
138+
}
139+
```
140+
141+
</section>
142+
143+
<!-- /.examples -->
144+
145+
<!-- 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. -->
146+
147+
<section class="references">
148+
149+
</section>
150+
151+
<!-- /.references -->
152+
153+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
154+
155+
<section class="links">
156+
157+
[oeis-a000583]: https://oeis.org/A000583
158+
159+
</section>
160+
161+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 isnan = require( '@stdlib/math/base/assert/is-nan' );
25+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
26+
var pkg = require( './../package.json' ).name;
27+
var iterFourthPowersSeq = require( './../lib' );
28+
29+
30+
// MAIN //
31+
32+
bench( pkg, function benchmark( b ) {
33+
var iter;
34+
var i;
35+
36+
b.tic();
37+
for ( i = 0; i < b.iterations; i++ ) {
38+
iter = iterFourthPowersSeq();
39+
if ( typeof iter !== 'object' ) {
40+
b.fail( 'should return an object' );
41+
}
42+
}
43+
b.toc();
44+
if ( !isIteratorLike( iter ) ) {
45+
b.fail( 'should return an iterator protocol-compliant object' );
46+
}
47+
b.pass( 'benchmark finished' );
48+
b.end();
49+
});
50+
51+
bench( pkg+'::iteration', function benchmark( b ) {
52+
var iter;
53+
var z;
54+
var i;
55+
56+
iter = iterFourthPowersSeq();
57+
58+
b.tic();
59+
for ( i = 0; i < b.iterations; i++ ) {
60+
z = iter.next().value;
61+
if ( isnan( z ) ) {
62+
b.fail( 'should not be NaN' );
63+
}
64+
}
65+
b.toc();
66+
if ( isnan( z ) ) {
67+
b.fail( 'should not be NaN' );
68+
}
69+
b.pass( 'benchmark finished' );
70+
b.end();
71+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
{{alias}}( [options] )
3+
Returns an iterator which generates a sequence of fourth powers.
4+
5+
If an environment supports Symbol.iterator, the returned iterator is
6+
iterable.
7+
8+
Parameters
9+
----------
10+
options: Object (optional)
11+
Function options.
12+
13+
options.iter: integer (optional)
14+
Number of iterations. Default: 9741.
15+
16+
Returns
17+
-------
18+
iterator: Object
19+
Iterator.
20+
21+
iterator.next(): Function
22+
Returns an iterator protocol-compliant object containing the next
23+
iterated value (if one exists) and a boolean flag indicating whether the
24+
iterator is finished.
25+
26+
iterator.return( [value] ): Function
27+
Finishes an iterator and returns a provided value.
28+
29+
Examples
30+
--------
31+
> var it = {{alias}}();
32+
> var v = it.next().value
33+
0
34+
> v = it.next().value
35+
1
36+
> v = it.next().value
37+
16
38+
39+
See Also
40+
--------
41+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 { Iterator as Iter, IterableIterator } from '@stdlib/types/iter';
24+
25+
// Define a union type representing both iterable and non-iterable iterators:
26+
type Iterator = Iter | IterableIterator;
27+
28+
/**
29+
* Interface describing function options.
30+
*/
31+
interface Options {
32+
/**
33+
* Number of iterations.
34+
*/
35+
iter?: number;
36+
}
37+
38+
/**
39+
* Returns an iterator which generates a sequence of fourth powers.
40+
*
41+
* ## Notes
42+
*
43+
* - If an environment supports `Symbol.iterator`, the returned iterator is iterable.
44+
*
45+
* @param options - function options
46+
* @param options.iter - number of iterations (default: 9741)
47+
* @throws `iter` option must be a nonnegative integer
48+
* @returns iterator
49+
*
50+
* @example
51+
* var iter = iterFourthPowersSeq();
52+
*
53+
* var v = iter.next().value;
54+
* // returns 0
55+
*
56+
* v = iter.next().value;
57+
* // returns 1
58+
*
59+
* v = iter.next().value;
60+
* // returns 16
61+
*
62+
* // ...
63+
*/
64+
declare function iterFourthPowersSeq( options?: Options ): Iterator;
65+
66+
67+
// EXPORTS //
68+
69+
export = iterFourthPowersSeq;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* @license Apache-2.0
3+
*
4+
* Copyright (c) 2020 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 iterFourthPowersSeq = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an iterator...
25+
{
26+
iterFourthPowersSeq(); // $ExpectType Iterator
27+
iterFourthPowersSeq( {} ); // $ExpectType Iterator
28+
iterFourthPowersSeq( { 'iter': 10 } ); // $ExpectType Iterator
29+
}
30+
31+
// The compiler throws an error if the function is provided a first argument which is not an options object...
32+
{
33+
iterFourthPowersSeq( null ); // $ExpectError
34+
}
35+
36+
// The compiler throws an error if the function is provided an `iter` option which is not a number...
37+
{
38+
iterFourthPowersSeq( { 'iter': '5' } ); // $ExpectError
39+
iterFourthPowersSeq( { 'iter': true } ); // $ExpectError
40+
iterFourthPowersSeq( { 'iter': false } ); // $ExpectError
41+
iterFourthPowersSeq( { 'iter': null } ); // $ExpectError
42+
iterFourthPowersSeq( { 'iter': [] } ); // $ExpectError
43+
iterFourthPowersSeq( { 'iter': {} } ); // $ExpectError
44+
iterFourthPowersSeq( { 'iter': ( x: number ): number => x } ); // $ExpectError
45+
}

0 commit comments

Comments
 (0)