README.md (6979B)
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 # iterMap3 22 23 > Create an [iterator][mdn-iterator-protocol] which invokes a ternary function accepting numeric arguments for each iterated value. 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 iterMap3 = require( '@stdlib/math/iter/tools/map3' ); 41 ``` 42 43 #### iterMap3( iter0, iter1, iter2, fcn\[, options] ) 44 45 Returns an [iterator][mdn-iterator-protocol] which invokes a ternary `function` accepting numeric arguments for each iterated value. 46 47 ```javascript 48 var array2iterator = require( '@stdlib/array/to-iterator' ); 49 var clamp = require( '@stdlib/math/base/special/clamp' ); 50 51 var x = array2iterator( [ 1.0, -2.0, 3.0, 14.0 ] ); 52 var min = array2iterator( [ 1.0, 0.0, -1.0, 1.0 ] ); 53 var max = array2iterator( [ 10.0, 10.0, 2.0, 10.0 ] ); 54 55 var it = iterMap3( x, min, max, clamp ); 56 // returns <Object> 57 58 var r = it.next().value; 59 // returns 1.0 60 61 r = it.next().value; 62 // returns 0.0 63 64 r = it.next().value; 65 // returns 2.0 66 67 // ... 68 ``` 69 70 The returned [iterator][mdn-iterator-protocol] protocol-compliant object has the following properties: 71 72 - **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. 73 - **return**: function which closes an [iterator][mdn-iterator-protocol] and returns a single (optional) argument in an [iterator][mdn-iterator-protocol] protocol-compliant object. 74 75 The invoked `function` is provided three arguments: 76 77 - `x`: iterated value from first input [iterator][mdn-iterator-protocol]. 78 - `y`: iterated value from second input [iterator][mdn-iterator-protocol]. 79 - `z`: iterated value from third input [iterator][mdn-iterator-protocol]. 80 81 ```javascript 82 var array2iterator = require( '@stdlib/array/to-iterator' ); 83 84 function fcn( x, y, z ) { 85 return x + y + z + 10; 86 } 87 88 var it1 = array2iterator( [ 1, 2, 3, 4 ] ); 89 var it2 = array2iterator( [ 1, 2, 3, 4 ] ); 90 var it3 = array2iterator( [ 1, 2, 3, 4 ] ); 91 92 var it = iterMap3( it1, it2, it3, fcn ); 93 // returns <Object> 94 95 var r = it.next().value; 96 // returns 13 97 98 r = it.next().value; 99 // returns 16 100 101 r = it.next().value; 102 // returns 19 103 104 // ... 105 ``` 106 107 The function supports the following `options`: 108 109 - **invalid**: return value when an input [iterator][mdn-iterator-protocol] yields a non-numeric value. Default: `NaN`. 110 111 By default, the function returns an [iterator][mdn-iterator-protocol] which returns `NaN` when an input [iterator][mdn-iterator-protocol] yields a non-numeric value. To specify a different return value, set the `invalid` option. 112 113 ```javascript 114 var array2iterator = require( '@stdlib/array/to-iterator' ); 115 var clamp = require( '@stdlib/math/base/special/clamp' ); 116 117 var it1 = array2iterator( [ '1.0', '2.0', '3.0' ] ); 118 var it2 = array2iterator( [ 0.0, 0.0, 0.0 ] ); 119 var it3 = array2iterator( [ 10.0, 10.0, 10.0 ] ); 120 121 var opts = { 122 'invalid': null 123 }; 124 var it = iterMap3( it1, it2, it3, clamp, opts ); 125 // returns <Object> 126 127 var v = it.next().value; 128 // returns null 129 130 v = it.next().value; 131 // returns null 132 133 // ... 134 ``` 135 136 If provided a numeric value as an [`iterator`][mdn-iterator-protocol] argument, the value is broadcast as an **infinite** [iterator][mdn-iterator-protocol] which **always** returns the provided value. 137 138 ```javascript 139 var array2iterator = require( '@stdlib/array/to-iterator' ); 140 var clamp = require( '@stdlib/math/base/special/clamp' ); 141 142 var x = array2iterator( [ -1.0, 20.0 ] ); 143 144 var it = iterMap3( x, 0.0, 10.0, clamp ); 145 // returns <Object> 146 147 var v = it.next().value; 148 // returns 0.0 149 150 v = it.next().value; 151 // returns 10.0 152 153 var bool = it.next().done; 154 // returns true 155 ``` 156 157 </section> 158 159 <!-- /.usage --> 160 161 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 162 163 <section class="notes"> 164 165 ## Notes 166 167 - 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. 168 - The length of the returned [iterator][mdn-iterator-protocol] is equal to the length of the shortest provided [iterator][mdn-iterator-protocol]. In other words, the returned [iterator][mdn-iterator-protocol] ends once **one** of the provided [iterators][mdn-iterator-protocol] ends. 169 - If an environment supports `Symbol.iterator` **and** a provided [iterator][mdn-iterator-protocol] is iterable, the returned [iterator][mdn-iterator-protocol] is iterable. 170 171 </section> 172 173 <!-- /.notes --> 174 175 <!-- Package usage examples. --> 176 177 <section class="examples"> 178 179 ## Examples 180 181 <!-- eslint no-undef: "error" --> 182 183 ```javascript 184 var uniform = require( '@stdlib/random/iter/uniform' ); 185 var clamp = require( '@stdlib/math/base/special/clamp' ); 186 var iterMap3 = require( '@stdlib/math/iter/tools/map3' ); 187 188 // Create seeded iterators for generating pseudorandom numbers: 189 var x = uniform( 0.0, 10.0, { 190 'seed': 1234, 191 'iter': 10 192 }); 193 194 var min = uniform( 0.0, 1.0, { 195 'seed': 4567, 196 'iter': 10 197 }); 198 199 var max = uniform( 9.0, 10.0, { 200 'seed': 7895, 201 'iter': 10 202 }); 203 204 // Create an iterator which consumes the pseudorandom number iterators: 205 var it = iterMap3( x, min, max, clamp ); 206 207 // Perform manual iteration... 208 var r; 209 while ( true ) { 210 r = it.next(); 211 if ( r.done ) { 212 break; 213 } 214 console.log( r.value ); 215 } 216 ``` 217 218 </section> 219 220 <!-- /.examples --> 221 222 <!-- 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. --> 223 224 <section class="references"> 225 226 </section> 227 228 <!-- /.references --> 229 230 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 231 232 <section class="links"> 233 234 [mdn-iterator-protocol]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol 235 236 </section> 237 238 <!-- /.links -->