Fifth v0.3.0 Release: Functional Programming

I am thrilled to announce a major update for Fifth: First-class support for Functional Programming!

Before today, Fifth was primarily an imperative, object-oriented/graph-oriented hybrid. While effective, I knew that handling data transformations cleanly required something more.

With the latest release, I’ve introduced three game-changing features: LambdasHigher-Order Functions, and Tail Call Optimization.

Lambdas: Code that moves with you

You no longer need to define a named function for every small operation. Anonymous functions (lambdas) let you define logic right where you need it.

// Define logic inline using a lambda!
apply(10, fun(x: int): int { return x * 2; });

Higher-Order Functions (HOFs)

Functions are now first-class citizens. You can assign them to variables, store them in lists, pass them as arguments, and return them from other functions.

I’ve introduced a clean arrow syntax for function types: [InputType] -> ReturnType.

map<TIn, TOut>(ts: [TIn], f: [TIn] -> TOut): [TOut]
{
    if(len(ts) == 0) {
        return [];
    }
    return [f(head(ts))] ++ map<TIn, TOut>(tail(ts), f);
}

main(): int
{
  ts: [int] = [1,2,3];
  map<int, string>(ts, fun(i: int):string  { return write(i); });
  ts2: [int] = map<int, int>(ts, fun(i: int): int { return i * 2; });
  return 0;
}

Recursion Without Fear (TCO)

We all love recursion, but we all hate StackOverflowException.

The Fifth compiler now includes a powerful Tail Call Optimization (TCO) pass. If you write a self-recursive function where the recursive call is in the tail position, the compiler invisibly rewrites it into a highly efficient while loop.

Go ahead, calculate the factorial of 1,000,000. Your stack is safe with us.

// Optimizes to a loop automatically!
factorial(n: int, acc: int): int {
    if (n <= 1) { return acc; }
    return factorial(n - 1, n * acc);
}

Getting Started

Check out the full Functional Programming Guide in our documentation to learn the syntax and details. Get the latest release here.

Happy Coding!