time-to-botec

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

main.js (6538B)


      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 isFunction = require( '@stdlib/assert/is-function' );
     25 var isIteratorLike = require( '@stdlib/assert/is-iterator-like' );
     26 var isNumber = require( '@stdlib/assert/is-number' ).isPrimitive;
     27 var hasOwnProp = require( '@stdlib/assert/has-own-property' );
     28 var iteratorSymbol = require( '@stdlib/symbol/iterator' );
     29 var validate = require( './validate.js' );
     30 
     31 
     32 // MAIN //
     33 
     34 /**
     35 * Returns an iterator which invokes a ternary function accepting numeric arguments for each iterated value.
     36 *
     37 * ## Notes
     38 *
     39 * -   When invoked, the input function is provided three arguments:
     40 *
     41 *     -   `x`: iterated value from first input iterator
     42 *     -   `y`: iterated value from second input iterator
     43 *     -   `z`: iterated value from third input iterator
     44 *
     45 * -   If provided a numeric value as an iterator argument, the value is broadcast as an **infinite** iterator which **always** returns the provided value.
     46 *
     47 * -   If an iterated value is non-numeric (including `NaN`), the returned iterator returns `NaN`. If non-numeric iterated values are possible, you are advised to provide an iterator which type checks and handles non-numeric values accordingly.
     48 *
     49 * -   The length of the returned iterator is equal to the length of the shortest provided iterator. In other words, the returned iterator ends once **one** of the provided iterators ends.
     50 *
     51 * -   If an environment supports `Symbol.iterator` and all provided iterators are iterable, the returned iterator is iterable.
     52 *
     53 * @param {Iterator} iter0 - first input iterator
     54 * @param {Iterator} iter1 - second input iterator
     55 * @param {Iterator} iter2 - third input iterator
     56 * @param {Function} fcn - function to invoke
     57 * @param {Options} [options] - options
     58 * @param {*} [options.invalid=NaN] - return value when an input iterator yields a non-numeric value
     59 * @throws {TypeError} first argument must be an iterator protocol-compliant object
     60 * @throws {TypeError} second argument must be an iterator protocol-compliant object
     61 * @throws {TypeError} third argument must be an iterator protocol-compliant object
     62 * @throws {TypeError} fourth argument must be a function
     63 * @throws {TypeError} options argument must be an object
     64 * @throws {TypeError} must provide valid options
     65 * @returns {Iterator} iterator
     66 *
     67 * @example
     68 * var uniform = require( '@stdlib/random/iter/uniform' );
     69 * var clamp = require( '@stdlib/math/base/special/clamp' );
     70 *
     71 * var x = uniform( 0.0, 10.0 );
     72 * var min = uniform( 0.0, 1.0 );
     73 * var max = uniform( 9.0, 10.0 );
     74 *
     75 * var iter = iterMap3( x, min, max, clamp );
     76 *
     77 * var r = iter.next().value;
     78 * // returns <number>
     79 *
     80 * r = iter.next().value;
     81 * // returns <number>
     82 *
     83 * r = iter.next().value;
     84 * // returns <number>
     85 *
     86 * // ...
     87 */
     88 function iterMap3( iter0, iter1, iter2, fcn, options ) {
     89 	var iterators;
     90 	var values;
     91 	var types;
     92 	var niter;
     93 	var iter;
     94 	var opts;
     95 	var FLG;
     96 	var err;
     97 	var i;
     98 
     99 	niter = 3;
    100 	values = [ 0.0, 0.0, 0.0 ];
    101 
    102 	iterators = [];
    103 	types = [];
    104 	for ( i = 0; i < niter; i++ ) {
    105 		iterators.push( arguments[ i ] );
    106 		if ( isIteratorLike( arguments[ i ] ) ) {
    107 			types.push( 1 );
    108 		} else if ( isNumber( arguments[ i ] ) ) {
    109 			types.push( 0 );
    110 		} else {
    111 			throw new TypeError( 'invalid argument. Must provide an iterator protocol-compliant object or a number. Argument: `' + i + '`. Value: `' + arguments[ i ] + '`.' );
    112 		}
    113 	}
    114 	if ( !isFunction( fcn ) ) {
    115 		throw new TypeError( 'invalid argument. Third argument must be a function. Value: `' + fcn + '`.' );
    116 	}
    117 	opts = {
    118 		'invalid': NaN
    119 	};
    120 	if ( arguments.length > 4 ) {
    121 		err = validate( opts, options );
    122 		if ( err ) {
    123 			throw err;
    124 		}
    125 	}
    126 	// Create an iterator protocol-compliant object:
    127 	iter = {};
    128 	setReadOnly( iter, 'next', next );
    129 	setReadOnly( iter, 'return', end );
    130 
    131 	// If an environment supports `Symbol.iterator` and all provided iterators are iterable, make the iterator iterable:
    132 	if ( iteratorSymbol ) {
    133 		for ( i = 0; i < niter; i++ ) {
    134 			if ( types[ i ] && !isFunction( iterators[ i ][ iteratorSymbol ] ) ) { // eslint-disable-line max-len
    135 				FLG = true;
    136 				break;
    137 			}
    138 		}
    139 		if ( !FLG ) {
    140 			setReadOnly( iter, iteratorSymbol, factory );
    141 		}
    142 	}
    143 	FLG = 0;
    144 	return iter;
    145 
    146 	/**
    147 	* Returns an iterator protocol-compliant object containing the next iterated value.
    148 	*
    149 	* @private
    150 	* @returns {Object} iterator protocol-compliant object
    151 	*/
    152 	function next() {
    153 		var err;
    154 		var v;
    155 		var i;
    156 		if ( FLG ) {
    157 			return {
    158 				'done': true
    159 			};
    160 		}
    161 		FLG = 0;
    162 		err = 0;
    163 		for ( i = 0; i < niter; i++ ) {
    164 			if ( types[ i ] ) {
    165 				v = iterators[ i ].next();
    166 				if ( v.done ) {
    167 					FLG += 1;
    168 					if ( hasOwnProp( v, 'value' ) ) {
    169 						if ( typeof v.value === 'number' ) {
    170 							values[ i ] = v.value;
    171 						} else {
    172 							err = 1;
    173 						}
    174 						continue;
    175 					}
    176 					return {
    177 						'done': true
    178 					};
    179 				}
    180 				if ( typeof v.value === 'number' ) {
    181 					values[ i ] = v.value;
    182 				} else {
    183 					err = 1;
    184 				}
    185 			} else {
    186 				values[ i ] = iterators[ i ];
    187 			}
    188 		}
    189 		if ( err ) {
    190 			return {
    191 				'value': opts.invalid,
    192 				'done': false
    193 			};
    194 		}
    195 		return {
    196 			'value': fcn( values[ 0 ], values[ 1 ], values[ 2 ] ),
    197 			'done': false
    198 		};
    199 	}
    200 
    201 	/**
    202 	* Finishes an iterator.
    203 	*
    204 	* @private
    205 	* @param {*} [value] - value to return
    206 	* @returns {Object} iterator protocol-compliant object
    207 	*/
    208 	function end( value ) {
    209 		FLG = 1;
    210 		if ( arguments.length ) {
    211 			return {
    212 				'value': value,
    213 				'done': true
    214 			};
    215 		}
    216 		return {
    217 			'done': true
    218 		};
    219 	}
    220 
    221 	/**
    222 	* Returns a new iterator.
    223 	*
    224 	* @private
    225 	* @returns {Iterator} iterator
    226 	*/
    227 	function factory() {
    228 		var args;
    229 		var i;
    230 
    231 		args = [];
    232 		for ( i = 0; i < niter; i++ ) {
    233 			if ( types[ i ] ) {
    234 				args.push( iterators[ i ][ iteratorSymbol ]() );
    235 			} else {
    236 				args.push( iterators[ i ] );
    237 			}
    238 		}
    239 		args.push( fcn, opts );
    240 		return iterMap3.apply( null, args );
    241 	}
    242 }
    243 
    244 
    245 // EXPORTS //
    246 
    247 module.exports = iterMap3;