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

outdatedDart 1 only

A library for trampolining tail calls

Trampolines for Tail Recursion #

Two functions and one method: tailcall, done, and compute. This is no rocket science but it is boilerplate code for the Dart functional programmer that can be handy to have around as a library.

Note however that this is probably not the recommended way of programming in Dart. This is still useful for porting libraries that make heavy use of mutual tail recursion to Dart.

import 'package:trampolines/trampolines.dart';

class Defs {
  static odd(n) => n == 0 ? done(false) : tailcall(() => even(n-1));
  static even(n) => n == 0 ? done(true) : tailcall(() => odd(n-1));

  static badodd(n) => n == 0 ? false : badeven(n-1);
  static badeven(n) => n == 0 ? true : badodd(n-1);
}

main() {
  print(Defs.even(100000).compute());
  try {
    print(Defs.badeven(100000));
  } on StackOverflowError catch (_) {
    print("You should have used dart-trampolines!");
  }
}

Try it! #

git clone https://code.google.com/p/dart-trampolines/
cd dart-trampolines
pub install
dart example/demo.dart
0
likes
0
points
11
downloads

Publisher

unverified uploader

Weekly Downloads

A library for trampolining tail calls

Homepage

License

unknown (license)

More

Packages that depend on trampolines