time-to-botec

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

main.js (3176B)


      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 FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' );
     26 var validate = require( './validate.js' );
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Returns an iterator which generates an interleaved sequence of even integers.
     33 *
     34 * ## Notes
     35 *
     36 * -   If an environment supports `Symbol.iterator`, the returned iterator is iterable.
     37 *
     38 * @param {Options} [options] - function options
     39 * @param {NonNegativeInteger} [options.iter=9007199254740992] - number of iterations
     40 * @throws {TypeError} options argument must be an object
     41 * @throws {TypeError} must provide valid options
     42 * @returns {Iterator} iterator
     43 *
     44 * @example
     45 * var iter = iterEvenIntegersSeq();
     46 *
     47 * var v = iter.next().value;
     48 * // returns 0
     49 *
     50 * v = iter.next().value;
     51 * // returns 2
     52 *
     53 * v = iter.next().value;
     54 * // returns -2
     55 *
     56 * // ...
     57 */
     58 function iterEvenIntegersSeq( options ) {
     59 	var opts;
     60 	var iter;
     61 	var FLG;
     62 	var err;
     63 	var sgn;
     64 	var i;
     65 	var j;
     66 
     67 	opts = {
     68 		'iter': FLOAT64_MAX_SAFE_INTEGER
     69 	};
     70 	if ( arguments.length ) {
     71 		err = validate( opts, options );
     72 		if ( err ) {
     73 			throw err;
     74 		}
     75 	}
     76 	sgn = -1;
     77 	i = 0;
     78 	j = 0;
     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 		i += 1;
     99 		if ( FLG || i > opts.iter ) {
    100 			return {
    101 				'done': true
    102 			};
    103 		}
    104 		if ( i === 1 ) {
    105 			return {
    106 				'value': 0,
    107 				'done': false
    108 			};
    109 		}
    110 		// Increment every other iteration...
    111 		if ( sgn < 0 ) {
    112 			j += 2;
    113 		}
    114 		sgn *= -1;
    115 		return {
    116 			'value': sgn * j,
    117 			'done': false
    118 		};
    119 	}
    120 
    121 	/**
    122 	* Finishes an iterator.
    123 	*
    124 	* @private
    125 	* @param {*} [value] - value to return
    126 	* @returns {Object} iterator protocol-compliant object
    127 	*/
    128 	function end( value ) {
    129 		FLG = true;
    130 		if ( arguments.length ) {
    131 			return {
    132 				'value': value,
    133 				'done': true
    134 			};
    135 		}
    136 		return {
    137 			'done': true
    138 		};
    139 	}
    140 
    141 	/**
    142 	* Returns a new iterator.
    143 	*
    144 	* @private
    145 	* @returns {Iterator} iterator
    146 	*/
    147 	function factory() {
    148 		return iterEvenIntegersSeq( opts );
    149 	}
    150 }
    151 
    152 
    153 // EXPORTS //
    154 
    155 module.exports = iterEvenIntegersSeq;