Skip to content

Instantly share code, notes, and snippets.

@DanielCardonaRojas
Created May 29, 2020 00:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanielCardonaRojas/cad2b5d8b6890d99923c259c11bc6ddb to your computer and use it in GitHub Desktop.
Save DanielCardonaRojas/cad2b5d8b6890d99923c259c11bc6ddb to your computer and use it in GitHub Desktop.
Function generators dart
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