time-to-botec

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

main.js (2999B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2020 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 setReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
     24 var iteratorSymbol = require( '@stdlib/symbol/iterator' );
     25 var validate = require( './validate.js' );
     26 
     27 
     28 // VARIABLES //
     29 
     30 var MAX_ITER = 11585; // sqrt(floor(sqrt(FLOAT64_MAX_SAFE_INTEGER*2)))
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Returns an iterator which generates a sequence of squared triangular numbers.
     37 *
     38 * ## Notes
     39 *
     40 * -   If an environment supports `Symbol.iterator`, the returned iterator is iterable.
     41 *
     42 * @param {Options} [options] - function options
     43 * @param {NonNegativeInteger} [options.iter=11585] - number of iterations
     44 * @throws {TypeError} options argument must be an object
     45 * @throws {TypeError} must provide valid options
     46 * @returns {Iterator} iterator
     47 *
     48 * @example
     49 * var iter = iterSquaredTriangularSeq();
     50 *
     51 * var v = iter.next().value;
     52 * // returns 0
     53 *
     54 * v = iter.next().value;
     55 * // returns 1
     56 *
     57 * v = iter.next().value;
     58 * // returns 9
     59 *
     60 * // ...
     61 */
     62 function iterSquaredTriangularSeq( options ) {
     63 	var opts;
     64 	var iter;
     65 	var FLG;
     66 	var err;
     67 	var i;
     68 
     69 	opts = {
     70 		'iter': MAX_ITER
     71 	};
     72 	if ( arguments.length ) {
     73 		err = validate( opts, options );
     74 		if ( err ) {
     75 			throw err;
     76 		}
     77 	}
     78 	i = -1;
     79 
     80 	// Create an iterator protocol-compliant object:
     81 	iter = {};
     82 	setReadOnly( iter, 'next', next );
     83 	setReadOnly( iter, 'return', end );
     84 
     85 	// If an environment supports `Symbol.iterator`, make the iterator iterable:
     86 	if ( iteratorSymbol ) {
     87 		setReadOnly( iter, iteratorSymbol, factory );
     88 	}
     89 	return iter;
     90 
     91 	/**
     92 	* Returns an iterator protocol-compliant object containing the next iterated value.
     93 	*
     94 	* @private
     95 	* @returns {Object} iterator protocol-compliant object
     96 	*/
     97 	function next() {
     98 		var x;
     99 		i += 1;
    100 		if ( FLG || i >= opts.iter ) {
    101 			return {
    102 				'done': true
    103 			};
    104 		}
    105 		x = (i/2.0)*(i+1);
    106 		return {
    107 			'value': x*x,
    108 			'done': false
    109 		};
    110 	}
    111 
    112 	/**
    113 	* Finishes an iterator.
    114 	*
    115 	* @private
    116 	* @param {*} [value] - value to return
    117 	* @returns {Object} iterator protocol-compliant object
    118 	*/
    119 	function end( value ) {
    120 		FLG = true;
    121 		if ( arguments.length ) {
    122 			return {
    123 				'value': value,
    124 				'done': true
    125 			};
    126 		}
    127 		return {
    128 			'done': true
    129 		};
    130 	}
    131 
    132 	/**
    133 	* Returns a new iterator.
    134 	*
    135 	* @private
    136 	* @returns {Iterator} iterator
    137 	*/
    138 	function factory() {
    139 		return iterSquaredTriangularSeq( opts );
    140 	}
    141 }
    142 
    143 
    144 // EXPORTS //
    145 
    146 module.exports = iterSquaredTriangularSeq;