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