Skip to content

Commit 93c9d2e

Browse files
committed
Add iterator for generating a sequence of negative integers
1 parent 6bbd7a4 commit 93c9d2e

12 files changed

Lines changed: 1099 additions & 0 deletions

File tree

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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+
# iterNegativeIntegers
22+
23+
> Create an iterator which generates a [negative integer sequence][oeis-a001478].
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 iterNegativeIntegers = require( '@stdlib/math/iter/negative-integers' );
41+
```
42+
43+
#### iterNegativeIntegers( \[options] )
44+
45+
Returns an iterator which generates a negative integer sequence.
46+
47+
```javascript
48+
var it = iterNegativeIntegers();
49+
// returns <Object>
50+
51+
var v = it.next().value;
52+
// returns -1
53+
54+
v = it.next().value;
55+
// returns -2
56+
57+
v = it.next().value;
58+
// returns -3
59+
60+
// ...
61+
```
62+
63+
The returned iterator protocol-compliant object has the following properties:
64+
65+
- **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.
66+
- **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object.
67+
68+
The function supports the following `options`:
69+
70+
- **iter**: number of iterations. Default: `9007199254740991`.
71+
72+
By default, the function returns an infinite iterator (i.e., an iterator which never ends). To limit the number of iterations, set the `iter` option.
73+
74+
```javascript
75+
var opts = {
76+
'iter': 2
77+
};
78+
var it = iterNegativeIntegers( opts );
79+
// returns <Object>
80+
81+
var v = it.next().value;
82+
// returns -1
83+
84+
v = it.next().value;
85+
// returns -2
86+
87+
var bool = it.next().done;
88+
// returns true
89+
```
90+
91+
</section>
92+
93+
<!-- /.usage -->
94+
95+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
96+
97+
<section class="notes">
98+
99+
## Notes
100+
101+
- If an environment supports `Symbol.iterator`, the returned iterator is iterable.
102+
103+
</section>
104+
105+
<!-- /.notes -->
106+
107+
<!-- Package usage examples. -->
108+
109+
<section class="examples">
110+
111+
## Examples
112+
113+
<!-- eslint no-undef: "error" -->
114+
115+
```javascript
116+
var iterNegativeIntegers = require( '@stdlib/math/iter/negative-integers' );
117+
118+
// Create an iterator:
119+
var opts = {
120+
'iter': 100
121+
};
122+
var it = iterNegativeIntegers( opts );
123+
124+
// Perform manual iteration...
125+
var v;
126+
while ( true ) {
127+
v = it.next();
128+
if ( v.done ) {
129+
break;
130+
}
131+
console.log( v.value );
132+
}
133+
```
134+
135+
</section>
136+
137+
<!-- /.examples -->
138+
139+
<!-- 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. -->
140+
141+
<section class="references">
142+
143+
</section>
144+
145+
<!-- /.references -->
146+
147+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
148+
149+
<section class="links">
150+
151+
[oeis-a001478]: https://oeis.org/A001478
152+
153+
</section>
154+
155+
<!-- /.links -->
Lines changed: 71 additions & 0 deletions
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 iterNegativeIntegers = 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 = iterNegativeIntegers();
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 = iterNegativeIntegers();
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+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
2+
{{alias}}( [options] )
3+
Returns an iterator which generates a negative integer sequence.
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: 9007199254740991.
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+
-1
34+
> v = it.next().value
35+
-2
36+
37+
See Also
38+
--------
39+
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
interface Options {
29+
/**
30+
* Number of iterations.
31+
*/
32+
iter?: number;
33+
}
34+
35+
/**
36+
* Returns an iterator which generates a negative integer sequence.
37+
*
38+
* ## Notes
39+
*
40+
* - If an environment supports `Symbol.iterator`, the returned iterator is iterable.
41+
*
42+
* @param options - function options
43+
* @param options.iter - number of iterations (default: 9007199254740991)
44+
* @throws `iter` option must be a nonnegative integer
45+
* @returns iterator
46+
*
47+
* @example
48+
* var iter = iterNegativeIntegers();
49+
*
50+
* var v = iter.next().value;
51+
* // returns -1
52+
*
53+
* v = iter.next().value;
54+
* // returns -2
55+
*
56+
* v = iter.next().value;
57+
* // returns -3
58+
*
59+
* // ...
60+
*/
61+
declare function iterNegativeIntegers( options?: Options ): Iterator;
62+
63+
64+
// EXPORTS //
65+
66+
export = iterNegativeIntegers;
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) 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 iterNegativeIntegers = require( './index' );
20+
21+
22+
// TESTS //
23+
24+
// The function returns an iterator...
25+
{
26+
iterNegativeIntegers(); // $ExpectType Iterator
27+
iterNegativeIntegers( {} ); // $ExpectType Iterator
28+
iterNegativeIntegers( { '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+
iterNegativeIntegers( 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+
iterNegativeIntegers( { 'iter': '5' } ); // $ExpectError
39+
iterNegativeIntegers( { 'iter': true } ); // $ExpectError
40+
iterNegativeIntegers( { 'iter': false } ); // $ExpectError
41+
iterNegativeIntegers( { 'iter': null } ); // $ExpectError
42+
iterNegativeIntegers( { 'iter': [] } ); // $ExpectError
43+
iterNegativeIntegers( { 'iter': {} } ); // $ExpectError
44+
iterNegativeIntegers( { 'iter': ( x: number ): number => x } ); // $ExpectError
45+
}

0 commit comments

Comments
 (0)