main.js (3032B)
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 isComposite = require( './../../../../base/assert/is-composite' ); 26 var validate = require( './validate.js' ); 27 28 29 // VARIABLES // 30 31 var MAX_ITER = 1e308; 32 33 34 // MAIN // 35 36 /** 37 * Returns an iterator which generates a sequence of composite numbers. 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=1e308] - 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 = iterCompositesSeq(); 51 * 52 * var v = iter.next().value; 53 * // returns 4 54 * 55 * v = iter.next().value; 56 * // returns 6 57 * 58 * v = iter.next().value; 59 * // returns 8 60 * 61 * // ... 62 */ 63 function iterCompositesSeq( options ) { 64 var opts; 65 var iter; 66 var FLG; 67 var err; 68 var n; 69 var i; 70 71 opts = { 72 'iter': MAX_ITER 73 }; 74 if ( arguments.length ) { 75 err = validate( opts, options ); 76 if ( err ) { 77 throw err; 78 } 79 } 80 i = 0; 81 n = 3; 82 83 // Create an iterator protocol-compliant object: 84 iter = {}; 85 setReadOnly( iter, 'next', next ); 86 setReadOnly( iter, 'return', end ); 87 88 // If an environment supports `Symbol.iterator`, make the iterator iterable: 89 if ( iteratorSymbol ) { 90 setReadOnly( iter, iteratorSymbol, factory ); 91 } 92 return iter; 93 94 /** 95 * Returns an iterator protocol-compliant object containing the next iterated value. 96 * 97 * @private 98 * @returns {Object} iterator protocol-compliant object 99 */ 100 function next() { 101 i += 1; 102 if ( FLG || i > opts.iter ) { 103 return { 104 'done': true 105 }; 106 } 107 n += 1; 108 while ( isComposite( n ) === false ) { 109 n += 1; 110 } 111 return { 112 'value': n, 113 'done': false 114 }; 115 } 116 117 /** 118 * Finishes an iterator. 119 * 120 * @private 121 * @param {*} [value] - value to return 122 * @returns {Object} iterator protocol-compliant object 123 */ 124 function end( value ) { 125 FLG = true; 126 if ( arguments.length ) { 127 return { 128 'value': value, 129 'done': true 130 }; 131 } 132 return { 133 'done': true 134 }; 135 } 136 137 /** 138 * Returns a new iterator. 139 * 140 * @private 141 * @returns {Iterator} iterator 142 */ 143 function factory() { 144 return iterCompositesSeq( opts ); 145 } 146 } 147 148 149 // EXPORTS // 150 151 module.exports = iterCompositesSeq;