tailcalls 0.1.0

  • README.md
  • Changelog.md
  • Example
  • Installing
  • Versions
  • 64

tailcall

A library for trampolining tail calls.

Example 1:

import 'package:tailcalls/tailcalls.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() {
  var res = fib(20).result();
  print("Result: $res");
}

Example 2:

TailRec<bool> isEven(List<int> xs) {
  if (xs.isEmpty) {
    return done(true);
  } else {
    return tailcall(() => isOdd(xs.sublist(1)));
  }
}

TailRec<bool> isOdd(List<int> xs) {
  if (xs.isEmpty) {
    return done(false);
  } else {
    return tailcall(() => isEven(xs.sublist(1)));
  }
}

void main() {
  List<int> r = rangeList(1, 40002);
  print(r.last);
  bool res = isEven(r).result();
  print(res);
}

Read more about trampoling in computing on Wikipedia.

Changelog

0.0.1

-Initial release

0.0.2

  • Updated to use Dart 2.1.0

0.0.3

  • 2 Examples added.

0.1.0

  • Fixed minor issues discovered by pana.

example/example.dart

import '../lib/tailcalls.dart';
import '../lib/utils.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> isEven(List<int> xs) {
  if (xs.isEmpty) {
    return done(true);
  } else {
    return tailcall(() => isOdd(xs.sublist(1)));
  }
}

TailRec<bool> isOdd(List<int> xs) {
  if (xs.isEmpty) {
    return done(false);
  } else {
    return tailcall(() => isEven(xs.sublist(1)));
  }
}

void main() {
  // Example 1
  var res = fib(20).result();
  print("Result: $res");

  // Example 2
  List<int> r = rangeList(1, 40002);
  print(r.last);
  bool res2 = isEven(r).result();
  print(res2);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  tailcalls: ^0.1.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:tailcalls/tailcalls.dart';
  
Version Uploaded Documentation Archive
0.1.0 Sep 28, 2018 Go to the documentation of tailcalls 0.1.0 Download tailcalls 0.1.0 archive
0.0.3 Sep 28, 2018 Go to the documentation of tailcalls 0.0.3 Download tailcalls 0.0.3 archive
0.0.2 Sep 28, 2018 Go to the documentation of tailcalls 0.0.2 Download tailcalls 0.0.2 archive
0.0.1 Dec 14, 2017 Go to the documentation of tailcalls 0.0.1 Download tailcalls 0.0.1 archive
Popularity:
Describes how popular the package is relative to other packages. [more]
28
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
64
Learn more about scoring.

We analyzed this package on Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:tailcalls/tailcalls.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <=2.1.0