https://github.com/preprocess/pre-short-closures
https://github.com/preprocess/pre-short-closures
arrow closure macro preprocessor
Last synced: 7 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/preprocess/pre-short-closures
- Owner: preprocess
- License: mit
- Created: 2017-01-27T03:30:11.000Z (about 9 years ago)
- Default Branch: main
- Last Pushed: 2020-06-18T16:16:39.000Z (over 5 years ago)
- Last Synced: 2025-11-27T16:21:56.018Z (2 months ago)
- Topics: arrow, closure, macro, preprocessor
- Language: Ruby
- Size: 35.2 KB
- Stars: 35
- Watchers: 1
- Forks: 5
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- License: license.md
- Code of conduct: code-of-conduct.md
Awesome Lists containing this project
README
# Short Closures
Documentation can be found at [preprocess.io](https://preprocess.io#short-closures).
> **Since [this RFC](https://wiki.php.net/rfc/arrow_functions_v2) 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:
```php
$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:
```php
$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"]);
```