Skip to content

Commit af2a343

Browse files
committed
Add iterator to round to the nearest power of 10 toward zero
1 parent 1a2dcda commit af2a343

File tree

10 files changed

+899
-0
lines changed

10 files changed

+899
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
# iterTrunc10
22+
23+
> Create an [iterator][mdn-iterator-protocol] which [rounds][@stdlib/math/base/special/trunc10] each iterated value to the nearest power of 10 toward zero.
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 iterTrunc10 = require( '@stdlib/math/iter/special/trunc10' );
41+
```
42+
43+
#### iterTrunc10( iterator )
44+
45+
Returns an [iterator][mdn-iterator-protocol] which [rounds][@stdlib/math/base/special/trunc10] each iterated value to the nearest power of `10` toward zero.
46+
47+
```javascript
48+
var array2iterator = require( '@stdlib/array/to-iterator' );
49+
50+
var it = iterTrunc10( array2iterator( [ 9.5, 13.0, -13.0 ] ) );
51+
// returns <Object>
52+
53+
var r = it.next().value;
54+
// returns 1.0
55+
56+
r = it.next().value;
57+
// returns 10.0
58+
59+
r = it.next().value;
60+
// returns -10.0
61+
62+
// ...
63+
```
64+
65+
The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties:
66+
67+
- **next**: function which returns an [iterator][mdn-iterator-protocol] 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.
68+
- **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object.
69+
70+
</section>
71+
72+
<!-- /.usage -->
73+
74+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
75+
76+
<section class="notes">
77+
78+
## Notes
79+
80+
- If an iterated value is non-numeric (including `NaN`), the returned [iterator][mdn-iterator-protocol] returns `NaN`. If non-numeric iterated values are possible, you are advised to provide an [`iterator`][mdn-iterator-protocol] which type checks and handles non-numeric values accordingly.
81+
- If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable.
82+
83+
</section>
84+
85+
<!-- /.notes -->
86+
87+
<!-- Package usage examples. -->
88+
89+
<section class="examples">
90+
91+
## Examples
92+
93+
<!-- eslint no-undef: "error" -->
94+
95+
```javascript
96+
var uniform = require( '@stdlib/random/iter/uniform' );
97+
var iterTrunc10 = require( '@stdlib/math/iter/special/trunc10' );
98+
99+
// Create a seeded iterator for generating pseudorandom numbers:
100+
var rand = uniform( -200.0, 200.0, {
101+
'seed': 1234,
102+
'iter': 10
103+
});
104+
105+
// Create an iterator which consumes the pseudorandom number iterator:
106+
var it = iterTrunc10( rand );
107+
108+
// Perform manual iteration...
109+
var r;
110+
while ( true ) {
111+
r = it.next();
112+
if ( r.done ) {
113+
break;
114+
}
115+
console.log( r.value );
116+
}
117+
```
118+
119+
</section>
120+
121+
<!-- /.examples -->
122+
123+
<!-- 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. -->
124+
125+
<section class="references">
126+
127+
</section>
128+
129+
<!-- /.references -->
130+
131+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
132+
133+
<section class="links">
134+
135+
[mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol
136+
137+
[@stdlib/math/base/special/trunc10]: https://github.com/stdlib-js/stdlib
138+
139+
</section>
140+
141+
<!-- /.links -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 randu = require( '@stdlib/random/iter/randu' );
25+
var isnan = require( '@stdlib/math/base/assert/is-nan' );
26+
var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
27+
var pkg = require( './../package.json' ).name;
28+
var iterTrunc10 = require( './../lib' );
29+
30+
31+
// MAIN //
32+
33+
bench( pkg, function benchmark( b ) {
34+
var rand;
35+
var iter;
36+
var i;
37+
38+
rand = randu();
39+
40+
b.tic();
41+
for ( i = 0; i < b.iterations; i++ ) {
42+
iter = iterTrunc10( rand );
43+
if ( typeof iter !== 'object' ) {
44+
b.fail( 'should return an object' );
45+
}
46+
}
47+
b.toc();
48+
if ( !isIteratorLike( iter ) ) {
49+
b.fail( 'should return an iterator protocol-compliant object' );
50+
}
51+
b.pass( 'benchmark finished' );
52+
b.end();
53+
});
54+
55+
bench( pkg+'::iteration', function benchmark( b ) {
56+
var rand;
57+
var iter;
58+
var z;
59+
var i;
60+
61+
rand = randu();
62+
iter = iterTrunc10( rand );
63+
64+
b.tic();
65+
for ( i = 0; i < b.iterations; i++ ) {
66+
z = iter.next().value;
67+
if ( isnan( z ) ) {
68+
b.fail( 'should not return NaN' );
69+
}
70+
}
71+
b.toc();
72+
if ( isnan( z ) ) {
73+
b.fail( 'should not return NaN' );
74+
}
75+
b.pass( 'benchmark finished' );
76+
b.end();
77+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
{{alias}}( iterator )
3+
Returns an iterator which rounds each iterated value to the nearest power of
4+
10 toward zero.
5+
6+
If an environment supports Symbol.iterator and a provided iterator is
7+
iterable, the returned iterator is iterable.
8+
9+
Parameters
10+
----------
11+
iterator: Object
12+
Input iterator.
13+
14+
Returns
15+
-------
16+
iterator: Object
17+
Iterator.
18+
19+
iterator.next(): Function
20+
Returns an iterator protocol-compliant object containing the next
21+
iterated value (if one exists) and a boolean flag indicating whether the
22+
iterator is finished.
23+
24+
iterator.return( [value] ): Function
25+
Finishes an iterator and returns a provided value.
26+
27+
Examples
28+
--------
29+
> var it = {{alias}}( {{alias:@stdlib/random/iter/uniform}}( -100.0, 100.0 ) );
30+
> var r = it.next().value
31+
<number>
32+
> r = it.next().value
33+
<number>
34+
35+
See Also
36+
--------
37+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
* Returns an iterator which rounds each iterated value to the nearest power of 10 toward zero.
30+
*
31+
* ## Notes
32+
*
33+
* - If an environment supports `Symbol.iterator` **and** a provided iterator is iterable, the returned iterator is iterable.
34+
*
35+
* @param iterator - input iterator
36+
* @returns iterator
37+
*
38+
* @example
39+
* var uniform = require( `@stdlib/random/iter/uniform` );
40+
*
41+
* var iter = iterTrunc10( uniform( -100.0, 100.0 ) );
42+
*
43+
* var r = iter.next().value;
44+
* // returns <number>
45+
*
46+
* r = iter.next().value;
47+
* // returns <number>
48+
*
49+
* r = iter.next().value;
50+
* // returns <number>
51+
*
52+
* // ...
53+
*/
54+
declare function iterTrunc10( iterator: Iterator ): Iterator;
55+
56+
57+
// EXPORTS //
58+
59+
export = iterTrunc10;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 iterTrunc10 = require( './index' );
20+
21+
/**
22+
* Returns an iterator protocol-compliant object.
23+
*
24+
* @returns iterator protocol-compliant object
25+
*/
26+
function iterator() {
27+
return {
28+
'next': next
29+
};
30+
31+
/**
32+
* Implements the iterator protocol `next` method.
33+
*
34+
* @returns iterator protocol-compliant object
35+
*/
36+
function next() {
37+
return {
38+
'value': true,
39+
'done': false
40+
};
41+
}
42+
}
43+
44+
45+
// TESTS //
46+
47+
// The function returns an iterator...
48+
{
49+
iterTrunc10( iterator() ); // $ExpectType Iterator
50+
}
51+
52+
// The compiler throws an error if the function is provided a first argument which is not an iterator protocol-compliant object...
53+
{
54+
iterTrunc10( '5' ); // $ExpectError
55+
iterTrunc10( 5 ); // $ExpectError
56+
iterTrunc10( true ); // $ExpectError
57+
iterTrunc10( false ); // $ExpectError
58+
iterTrunc10( null ); // $ExpectError
59+
iterTrunc10( undefined ); // $ExpectError
60+
iterTrunc10( [] ); // $ExpectError
61+
iterTrunc10( {} ); // $ExpectError
62+
iterTrunc10( ( x: number ): number => x ); // $ExpectError
63+
}
64+
65+
// The compiler throws an error if the function is provided insufficient arguments...
66+
{
67+
iterTrunc10(); // $ExpectError
68+
}

0 commit comments

Comments
 (0)