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

outdated

A library for trampolining recursive calls by hand in cases when optimization isn't done automatically. This library is ported from Scala's standard library.

trampoline #

A library for trampolining tail calls. Inspired by TailCalls.scala.

Example 1: #


import 'package:trampoline/trampoline.dart';

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);
      });
    });
  }
}

void main() {
  int z = 10;
  print("fib(${z}) is ${fib(z).result}!");
}

Example 2: #


import 'package:trampoline/trampoline.dart';

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 z = 1000;
  print("${z} is odd? ${odd(z).result}!");
}

Read more about trampoling in computing on Wikipedia.

1
likes
0
points
23
downloads

Publisher

verified publisherwelopment.com

Weekly Downloads

A library for trampolining recursive calls by hand in cases when optimization isn't done automatically. This library is ported from Scala's standard library.

Repository (GitHub)
View/report issues

License

unknown (license)

More

Packages that depend on trampoline