main.js (2940B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 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 isArrayLike = require( '@stdlib/assert/is-array-like-object' ); 24 var isPositiveZero = require( '@stdlib/math/base/assert/is-positive-zero' ); 25 var isNegativeZero = require( '@stdlib/math/base/assert/is-negative-zero' ); 26 var isnan = require( '@stdlib/math/base/assert/is-nan' ); 27 var PINF = require( '@stdlib/constants/float64/pinf' ); 28 var NINF = require( '@stdlib/constants/float64/ninf' ); 29 30 31 // MAIN // 32 33 /** 34 * Returns an accumulator function which incrementally computes minimum and maximum values. 35 * 36 * @param {Collection} [out] - output array 37 * @throws {TypeError} output argument must be array-like 38 * @returns {Function} accumulator function 39 * 40 * @example 41 * var accumulator = incrminmax(); 42 * 43 * var mm = accumulator(); 44 * // returns null 45 * 46 * mm = accumulator( 2.0 ); 47 * // returns [ 2.0, 2.0 ] 48 * 49 * mm = accumulator( -5.0 ); 50 * // returns [ -5.0, 2.0 ] 51 * 52 * mm = accumulator( 3.0 ); 53 * // returns [ -5.0, 3.0 ] 54 * 55 * mm = accumulator( 5.0 ); 56 * // returns [ -5.0, 5.0 ] 57 * 58 * mm = accumulator(); 59 * // returns [ -5.0, 5.0 ] 60 */ 61 function incrminmax( out ) { 62 var minmax; 63 var min; 64 var max; 65 var FLG; 66 if ( arguments.length === 0 ) { 67 minmax = [ 0.0, 0.0 ]; 68 } else { 69 if ( !isArrayLike( out ) ) { 70 throw new TypeError( 'invalid argument. Output argument must be an array-like object. Value: `' + out + '`.' ); 71 } 72 minmax = out; 73 } 74 min = PINF; 75 max = NINF; 76 FLG = false; 77 return accumulator; 78 79 /** 80 * If provided a value, the accumulator function returns updated minimum and maximum values. If not provided a value, the accumulator function returns the current minimum and maximum values. 81 * 82 * @private 83 * @param {number} [x] - input value 84 * @returns {(ArrayLikeObject|null)} output array or null 85 */ 86 function accumulator( x ) { 87 if ( arguments.length === 0 ) { 88 if ( FLG === false ) { 89 return null; 90 } 91 minmax[ 0 ] = min; // Why? Because we cannot guarantee someone hasn't mutated the output array 92 minmax[ 1 ] = max; 93 return minmax; 94 } 95 FLG = true; 96 if ( isnan( x ) ) { 97 min = x; 98 max = x; 99 } else { 100 if ( x < min || ( x === min && isNegativeZero( x ) ) ) { 101 min = x; 102 } 103 if ( x > max || ( x === max && isPositiveZero( x ) ) ) { 104 max = x; 105 } 106 } 107 minmax[ 0 ] = min; 108 minmax[ 1 ] = max; 109 return minmax; 110 } 111 } 112 113 114 // EXPORTS // 115 116 module.exports = incrminmax;