trampoline 0.0.1
trampoline: ^0.0.1 copied to clipboard
A library for trampolining tail calls by hand in cases when tail call optimization isn't done automatically. This library is ported from Scala's standard library (TailRec).
example/main.dart
import 'package:trampoline/trampoline.dart';
// Example 1
TailRec<int> fib(int n) {
if (n < 2) {
return done(n);
} else {
return tailcall(() => fib(n - 1)).flatMap((x) {
return tailcall(() => fib(n - 2)).map((y) {
return (x + y);
});
});
}
}
// Example 2
TailRec<bool> odd(int n) => n == 0 ? done(false) : tailcall(() => even(n - 1));
TailRec<bool> even(int n) => n == 0 ? done(true) : tailcall(() => odd(n - 1));
void main() {
int y = 1000;
print("${y} is odd? ${odd(y).result} !");
int z = 10;
print("fib of ${z} is ${fib(z).result} !");
}