README.md (5194B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2019 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # iterFibonacciSeq 22 23 > Create an iterator which generates a [Fibonacci sequence][fibonacci-number]. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 The [Fibonacci numbers][fibonacci-number] are the integer sequence 30 31 <!-- <equation class="equation" label="eq:fibonacci_sequence" align="center" raw="0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, \ldots" alt="Fibonacci sequence"> --> 32 33 <div class="equation" align="center" data-raw-text="0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, \ldots" data-equation="eq:fibonacci_sequence"> 34 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@aa77a2f6e76d2e9da5b49bffa45ee5167d6c16e1/lib/node_modules/@stdlib/math/iter/sequences/fibonacci/docs/img/equation_fibonacci_sequence.svg" alt="Fibonacci sequence"> 35 <br> 36 </div> 37 38 <!-- </equation> --> 39 40 The sequence is defined by the recurrence relation 41 42 <!-- <equation class="equation" label="eq:fibonacci_recurrence_relation" align="center" raw="F_n = F_{n-1} + F_{n-2}" alt="Fibonacci sequence recurrence relation"> --> 43 44 <div class="equation" align="center" data-raw-text="F_n = F_{n-1} + F_{n-2}" data-equation="eq:fibonacci_recurrence_relation"> 45 <img src="https://cdn.jsdelivr.net/gh/stdlib-js/stdlib@aa77a2f6e76d2e9da5b49bffa45ee5167d6c16e1/lib/node_modules/@stdlib/math/iter/sequences/fibonacci/docs/img/equation_fibonacci_recurrence_relation.svg" alt="Fibonacci sequence recurrence relation"> 46 <br> 47 </div> 48 49 <!-- </equation> --> 50 51 with seed values `F_0 = 0` and `F_1 = 1`. 52 53 </section> 54 55 <!-- /.intro --> 56 57 <!-- Package usage documentation. --> 58 59 <section class="usage"> 60 61 ## Usage 62 63 ```javascript 64 var iterFibonacciSeq = require( '@stdlib/math/iter/sequences/fibonacci' ); 65 ``` 66 67 #### iterFibonacciSeq( \[options] ) 68 69 Returns an iterator which generates a [Fibonacci sequence][fibonacci-number]. 70 71 ```javascript 72 var it = iterFibonacciSeq(); 73 // returns <Object> 74 75 var v = it.next().value; 76 // returns 0 77 78 v = it.next().value; 79 // returns 1 80 81 v = it.next().value; 82 // returns 1 83 84 // ... 85 ``` 86 87 The returned iterator protocol-compliant object has the following properties: 88 89 - **next**: function which returns an iterator protocol-compliant object containing the next iterated value (if one exists) assigned to a `value` property and a `done` property having a `boolean` value indicating whether the iterator is finished. 90 - **return**: function which closes an iterator and returns a single (optional) argument in an iterator protocol-compliant object. 91 92 The function supports the following `options`: 93 94 - **iter**: number of iterations. Default: `79`. 95 96 The returned iterator can only generate the first `79` [Fibonacci numbers][fibonacci-number], as larger [Fibonacci numbers][fibonacci-number] cannot be safely represented in [double-precision floating-point format][ieee754]. By default, the function returns an iterator which generates all `79` numbers. To limit the number of iterations, set the `iter` option. 97 98 ```javascript 99 var opts = { 100 'iter': 2 101 }; 102 var it = iterFibonacciSeq( opts ); 103 // returns <Object> 104 105 var v = it.next().value; 106 // returns 0 107 108 v = it.next().value; 109 // returns 1 110 111 var bool = it.next().done; 112 // returns true 113 ``` 114 115 </section> 116 117 <!-- /.usage --> 118 119 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 120 121 <section class="notes"> 122 123 ## Notes 124 125 - If an environment supports `Symbol.iterator`, the returned iterator is iterable. 126 127 </section> 128 129 <!-- /.notes --> 130 131 <!-- Package usage examples. --> 132 133 <section class="examples"> 134 135 ## Examples 136 137 <!-- eslint no-undef: "error" --> 138 139 ```javascript 140 var iterFibonacciSeq = require( '@stdlib/math/iter/sequences/fibonacci' ); 141 142 // Create an iterator: 143 var it = iterFibonacciSeq(); 144 145 // Perform manual iteration... 146 var v; 147 while ( true ) { 148 v = it.next(); 149 if ( v.done ) { 150 break; 151 } 152 console.log( v.value ); 153 } 154 ``` 155 156 </section> 157 158 <!-- /.examples --> 159 160 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 161 162 <section class="references"> 163 164 </section> 165 166 <!-- /.references --> 167 168 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 169 170 <section class="links"> 171 172 [fibonacci-number]: https://en.wikipedia.org/wiki/Fibonacci_number 173 174 [ieee754]: https://en.wikipedia.org/wiki/IEEE_754-1985 175 176 </section> 177 178 <!-- /.links -->