tailcalls 0.1.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 50

tailcalls #

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.

tailcall #

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 related to version 2.0 of Dart

0.1.1 #

  • Fixed minor issues discovered by pana related to version 2.1 of Dart

0.1.2 #

  • Fixed minor issues discovered by pana related to version 2.3 of Dart

0.1.3 #

  • Fixed minor issues discovered by pana related to version 2.4.1 of Dart

example/example.dart

import 'package:tailcalls/utils.dart';
import 'package:tailcalls/tailcalls.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)));
  }
}

int startRecursion(int n) {
  TailRec<int> recursiveFunction(int z) {
    if (z < n) {
      return tailcall(() => recursiveFunction(z + 1));
    } else {
      return done(z);
    }
  }

  return recursiveFunction(0) .result();
}

int startRecursion2(int n) {
  int recursiveFunction(int z) {
    if (z < n) {
      return recursiveFunction(z + 1);
    } else {
      return z;
    }
  }

  return recursiveFunction(0);
}

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

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



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

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

  // Example 3
  List<int> r4 = rangeList(1, 400);
  print(r4.last);
  bool res23 = isEven(r).result();
  print(res23);
  print("begin 1");
  var res3 = startRecursion(1000);
  print("end: " + res3.toString());
  print("begin 2");
  var res4 = startRecursion2(1000);
  print("end: " + res4.toString());

  // Example 4
  print((Defs.even(1000).result()));
}

Use this package as a library

1. Depend on it

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


dependencies:
  tailcalls: ^0.1.3

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub 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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
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]
50
Learn more about scoring.

We analyzed this package on Nov 21, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

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

Health issues and suggestions

Document public APIs. (-0.00 points)

28 out of 29 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Format lib/tailcalls.dart.

Run dartfmt to format lib/tailcalls.dart.

Format lib/utils.dart.

Run dartfmt to format lib/utils.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.8.0 <3.0.0
Dev dependencies
pedantic ^1.0.0
test 1.6.3