pre/short-closures

Installs: 480

Dependents: 0

Suggesters: 0

Security: 0

Stars: 35

Watchers: 2

Forks: 5

Open Issues: 2

Language:Ruby

Type:pre-macro

0.8.0 2019-05-02 12:23 UTC

This package is auto-updated.

Last update: 2024-03-19 00:21:39 UTC


README

Documentation can be found at preprocess.io.

Since this RFC was approved, we have standardised on the same syntax. We do still allow function bodies and return type hints, but everything in that RFC can be done, with the same syntax, as far back as Yay will allow (7.1).

You can use closures with similar semantics to Javascript:

$files = array_map(
    fn($path) => file_get_contents($path),
    $paths
);

$needles = [
    "PHP",
    "Go",
    "Javascript",
];

$matches = array_filter($files, fn($content = "") => {
    foreach ($needles as $needle) {
        if (stristr($content, $needle)) {
            return true;
        }
    }

    return false;
});

These are converted to:

$files = array_map(
    function ($path) {
        return file_get_contents($path);
    },
    $paths
);

$needles = [
    "PHP",
    "Go",
    "Javascript",
];

$matches = array_filter($files, [$needles = $needles ?? null, $needle = $needle ?? null, "fn" => function ($content = "") use (&$needles, &$needle) {
    foreach ($needles as $needle) {
        if (stristr($content, $needle)) {
            return true;
        }
    }

    return false;
}]["fn"]);