Created
May 29, 2020 00:36
-
-
Save DanielCardonaRojas/cad2b5d8b6890d99923c259c11bc6ddb to your computer and use it in GitHub Desktop.
Function generators dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
void main() { | |
var i = 20; | |
final fIterable = fibonacci(Fibonacci(0, 1)); | |
fIterable.take(30).forEach((element) { print(element)}); | |
print('fibonacci($i) = ${fibonacci(i)}'); | |
} | |
/// Computes the nth Fibonacci number. | |
Iterable<int> fibonacci(Fibonacci f) sync* { | |
yield f.current; | |
f.step(); | |
yield* fibo(f); | |
} | |
class Fibonacci { | |
int current; | |
int next; | |
Fibonacci(this.current, this.next); | |
factory Fibonacci.initial() { | |
return Fibonacci(current: 0, next: 0); | |
} | |
void step() { | |
final newNext = next + current; | |
current = next; | |
next = newNext; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment