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

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.

example/example.dart

import 'package:trampoline/trampoline.dart';


// example for 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 trampoline() {

  bool res1;
  res1 = even(101).result;
  print("Ergebnis von Odd/Even ist $res1");
  for (int z = 20; z < 35; z++) {
    num res2;
    res2 = fib(z).result;
    print("Ergebnis von Fibonacci für $z ist $res2");
  }
}
1
likes
110
pub points
0%
popularity

Publisher

verified publisherwelopment.com

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

Documentation

API reference

License

MIT (LICENSE)

More

Packages that depend on trampoline