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