time-to-botec

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

main.js (2900B)


      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 = 208063; // floor(cbrt(FLOAT64_MAX_SAFE_INTEGER))
     31 
     32 
     33 // MAIN //
     34 
     35 /**
     36 * Returns an iterator which generates a sequence of cubes.
     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=208063] - 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 = iterCubesSeq();
     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 8
     59 *
     60 * // ...
     61 */
     62 function iterCubesSeq( 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 		i += 1;
     99 		if ( FLG || i >= opts.iter ) {
    100 			return {
    101 				'done': true
    102 			};
    103 		}
    104 		return {
    105 			'value': i * i * i,
    106 			'done': false
    107 		};
    108 	}
    109 
    110 	/**
    111 	* Finishes an iterator.
    112 	*
    113 	* @private
    114 	* @param {*} [value] - value to return
    115 	* @returns {Object} iterator protocol-compliant object
    116 	*/
    117 	function end( value ) {
    118 		FLG = true;
    119 		if ( arguments.length ) {
    120 			return {
    121 				'value': value,
    122 				'done': true
    123 			};
    124 		}
    125 		return {
    126 			'done': true
    127 		};
    128 	}
    129 
    130 	/**
    131 	* Returns a new iterator.
    132 	*
    133 	* @private
    134 	* @returns {Iterator} iterator
    135 	*/
    136 	function factory() {
    137 		return iterCubesSeq( opts );
    138 	}
    139 }
    140 
    141 
    142 // EXPORTS //
    143 
    144 module.exports = iterCubesSeq;