README.md (3897B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2019 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 # itermrange 22 23 > Create an [iterator][mdn-iterator-protocol] which iteratively computes a moving [range][range]. 24 25 <section class="intro"> 26 27 The [**range**][range] is defined as the difference between the maximum and minimum values. 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var itermrange = require( '@stdlib/stats/iter/mrange' ); 41 ``` 42 43 #### itermrange( iterator, W ) 44 45 Returns an [iterator][mdn-iterator-protocol] which iteratively computes a moving [range][range]. The `W` parameter defines the number of iterated values over which to compute the moving [range][range]. 46 47 ```javascript 48 var array2iterator = require( '@stdlib/array/to-iterator' ); 49 50 var arr = array2iterator( [ 2.0, 1.0, 3.0, -7.0, -5.0 ] ); 51 var it = itermrange( arr, 3 ); 52 53 // Fill the window... 54 var v = it.next().value; // [2.0] 55 // returns 0.0 56 57 v = it.next().value; // [2.0, 1.0] 58 // returns 1.0 59 60 v = it.next().value; // [2.0, 1.0, 3.0] 61 // returns 2.0 62 63 // Window begins sliding... 64 v = it.next().value; // [1.0, 3.0, -7.0] 65 // returns 10.0 66 67 v = it.next().value; // [3.0, -7.0, -5.0] 68 // returns 10.0 69 ``` 70 71 </section> 72 73 <!-- /.usage --> 74 75 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 76 77 <section class="notes"> 78 79 ## Notes 80 81 - If an iterated value is non-numeric (including `NaN`), the function returns `NaN` for **at least** `W-1` future invocations. 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. 82 - As `W` values are needed to fill the window buffer, the first `W-1` returned values are calculated from smaller sample sizes. Until the window is full, each returned value is calculated from all previously iterated values. 83 84 </section> 85 86 <!-- /.notes --> 87 88 <!-- Package usage examples. --> 89 90 <section class="examples"> 91 92 ## Examples 93 94 <!-- eslint no-undef: "error" --> 95 96 ```javascript 97 var runif = require( '@stdlib/random/iter/uniform' ); 98 var itermrange = require( '@stdlib/stats/iter/mrange' ); 99 100 // Create an iterator for generating uniformly distributed pseudorandom numbers: 101 var rand = runif( -10.0, 10.0, { 102 'seed': 1234, 103 'iter': 100 104 }); 105 106 // Create an iterator for iteratively computing a moving range: 107 var it = itermrange( rand, 3 ); 108 109 // Perform manual iteration... 110 var v; 111 while ( true ) { 112 v = it.next(); 113 if ( typeof v.value === 'number' ) { 114 console.log( 'range: %d', v.value ); 115 } 116 if ( v.done ) { 117 break; 118 } 119 } 120 ``` 121 122 </section> 123 124 <!-- /.examples --> 125 126 <!-- 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. --> 127 128 <section class="references"> 129 130 </section> 131 132 <!-- /.references --> 133 134 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 135 136 <section class="links"> 137 138 [range]: https://en.wikipedia.org/wiki/Range_%28statistics%29 139 140 [mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol 141 142 </section> 143 144 <!-- /.links -->