md/lambda-preprocessor

Installs: 8

Dependents: 0

Suggesters: 0

Security: 0

Stars: 4

Watchers: 1

Forks: 0

Open Issues: 0

Language:Python

Type:pre-macro

0.1.0 2017-07-15 19:00 UTC

This package is not auto-updated.

Last update: 2024-11-19 17:39:45 UTC


README

In Hacklang, a lambda expression is denoted by using the lambda arrow ==>. Left of the arrow are arguments to the anonymous function and on the right hand side is either an expression or a list of statements.

This pre-processor is meant to make that syntax available in native PHP applications. This does not happen at runtime, so there is no hit on perfomance. The pre-processor works on top of the pre plugin and the yay macro library.

Syntax wise, and due to the fact that Yay does not have an expresison parser yet, even though Hack supports a single expression to be written without the need of curly brakets, this pre-processor will always require the curly brakets to be written. Hopefully this will change once Yay develops further.

Oneliner anonymous function

The simplest example is an anonymous function that returns the argument it was given in the first place. In PHP it could be written as:

$id = function($x) { return $x };

Which can be written, using this pre-processor, much simpler:

$id = $x ==> { $x };

Should we need to declare typehints or pass more than one argument, we will need to use parenthesis. We could also use parenthesis in the example above, but it is optional for single arguments with no type hinting.

$addNumbers = ($x, $y) ==> { $x + $y };

Which is translated to:

$addNumbers = function ($x, $y) { return $x + $y };

Or with type hints:

$addNumbers = (int $x, int $y): int ==> { $x + $y };

Which is translated to:

$addNumbers = function (int $x, int $y): int { return $x + $y };

Multiple lines anonymous functions

The same rules for arguments, typehints and return types still apply for anonymous functions with multiple lines. The only difference is that you will need to write the return keyword in the last statement of the block, and you must now end every statement with a semicolon.

$increment = $x ==> {
    $y = $x + 1;
    return $y;
};

This is translated into:

$increment = function($x) {
    $y = $x + 1;
    return $y;
};

Closures

PHP provides the keyword use to capture variables from the enclosing scope. Hack's lambdas make this even easier. You can use variables implicitly inside of a lambda. This is available when you use this pre-processor.

function addLastname(array $names, string $lastname): array
{
    return array_map($name ==> { $name . " " . $lastname }, $names);
}

Note that to use $lastname with the official syntax you would have to use the use keyword, as shown below:

function addLastname(array $names, string $lastname): array
{
    return array_map(function($name) use ($lastname) { return $name . " " . $lastname }, $names);
}

Recursive calls and precedence

The pre-processor will translate the inner block and then the outer. The precedence with this syntax is a bit more obvious than the hack one. In hack you can ommit the brakets {, }. Here, it should be straightforward that the expression:

$lambda = $x ==> { $y ==> { $x + $y }; };

will be translated into:

$lambda = function($x) { return function($y) { return $x + $y; }; };

Final Notes

  • This pre-processor is alpha software. Do not use it in production.