main.js (1698B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2019 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 isIteratorLike = require( '@stdlib/assert/is-iterator-like' ); 24 var hasOwnProp = require( '@stdlib/assert/has-own-property' ); 25 var incrmidrange = require( './../../../incr/midrange' ); 26 27 28 // MAIN // 29 30 /** 31 * Computes the mid-range of all iterated values. 32 * 33 * @param {Iterator} iterator - input iterator 34 * @throws {TypeError} must provide an iterator 35 * @returns {(number|null)} mid-range 36 * 37 * @example 38 * var runif = require( '@stdlib/random/iter/uniform' ); 39 * 40 * var rand = runif( -10.0, 10.0, { 41 * 'iter': 100 42 * }); 43 * 44 * var v = itermidrange( rand ); 45 * // returns <number> 46 */ 47 function itermidrange( iterator ) { 48 var acc; 49 var v; 50 if ( !isIteratorLike( iterator ) ) { 51 throw new TypeError( 'invalid argument. Must provide an iterator. Value: `'+iterator+'`.' ); 52 } 53 acc = incrmidrange(); 54 while ( true ) { 55 v = iterator.next(); 56 if ( typeof v.value === 'number' ) { 57 acc( v.value ); 58 } else if ( hasOwnProp( v, 'value' ) ) { 59 acc( NaN ); 60 } 61 if ( v.done ) { 62 break; 63 } 64 } 65 return acc(); 66 } 67 68 69 // EXPORTS // 70 71 module.exports = itermidrange;