main.js (3313B)
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 F0 = 0; 31 var F1 = 1; 32 33 34 // MAIN // 35 36 /** 37 * Returns an iterator which generates a negaFibonacci sequence. 38 * 39 * ## Notes 40 * 41 * - The returned iterator can only generate the first `79` negaFibonacci numbers, as larger negaFibonacci 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=79] - 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 `79` 49 * @returns {Iterator} iterator 50 * 51 * @example 52 * var iter = iterNegaFibonacciSeq(); 53 * 54 * var v = iter.next().value; 55 * // returns 0 56 * 57 * v = iter.next().value; 58 * // returns 1 59 * 60 * v = iter.next().value; 61 * // returns -1 62 * 63 * // ... 64 */ 65 function iterNegaFibonacciSeq( options ) { 66 var opts; 67 var iter; 68 var FLG; 69 var err; 70 var f1; 71 var f2; 72 var f; 73 var i; 74 75 opts = { 76 'iter': 79 77 }; 78 if ( arguments.length ) { 79 err = validate( opts, options ); 80 if ( err ) { 81 throw err; 82 } 83 } 84 f1 = F0; 85 f2 = F1; 86 f = 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 f = F0; 115 } else if ( i === 2 ) { 116 f = F1; 117 } else { 118 f = f1 - f2; 119 f1 = f2; 120 f2 = f; 121 } 122 return { 123 'value': f, 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 iterNegaFibonacciSeq( opts ); 156 } 157 } 158 159 160 // EXPORTS // 161 162 module.exports = iterNegaFibonacciSeq;