trampoline 0.0.1 copy "trampoline: ^0.0.1" to clipboard
trampoline: ^0.0.1 copied to clipboard

outdated

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} !");
}
1
likes
0
points
23
downloads

Publisher

verified publisherwelopment.com

Weekly Downloads

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).

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on trampoline