time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

main.js (1543B)


      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 // MAIN //
     22 
     23 /**
     24 * Returns an accumulator function which incrementally computes a sum of squared absolute values.
     25 *
     26 * @returns {Function} accumulator function
     27 *
     28 * @example
     29 * var accumulator = incrsumabs2();
     30 *
     31 * var sum = accumulator();
     32 * // returns null
     33 *
     34 * sum = accumulator( 2.0 );
     35 * // returns 4.0
     36 *
     37 * sum = accumulator( -5.0 );
     38 * // returns 29.0
     39 *
     40 * sum = accumulator();
     41 * // returns 29.0
     42 */
     43 function incrsumabs2() {
     44 	var sum = 0.0;
     45 	var FLG;
     46 	return accumulator;
     47 
     48 	/**
     49 	* If provided a value, the accumulator function returns an updated sum. If not provided a value, the accumulator function returns the current sum.
     50 	*
     51 	* @private
     52 	* @param {number} [x] - new value
     53 	* @returns {(number|null)} sum or null
     54 	*/
     55 	function accumulator( x ) {
     56 		if ( arguments.length === 0 ) {
     57 			return ( FLG ) ? sum : null;
     58 		}
     59 		FLG = true;
     60 		sum += x * x;
     61 		return sum;
     62 	}
     63 }
     64 
     65 
     66 // EXPORTS //
     67 
     68 module.exports = incrsumabs2;