Functional Programming Helper Functions and Classes
Functional Programming Helper Functions and Classes
Ancarda\Functional (also known as “FP Kit“) is a collection of helper functions and classes for PHP 7.0+ that makes basic Functional Programming a little easier. The Operation
class allows modification to data in a way that is more intuitive and understandable than a series of array_*
functions.
array_unique(
array_reduce(
array_map(
function (string $path): array {
return findLinks(__DIR__ . '/' . $path);
},
array_filter(
scandir(__DIR__),
function (string $path): bool {
return strpos($path, '.') !== 0;
}
)
),
function (?array $carry, array $row): array {
return array_merge($carry === null ? [] : $carry, $row);
}
)
);
This same operation can be done using an Operation
class. It’s far more readable and maintainable. Execution happens once the operation is realized.
(new \Ancarda\Functional\Operation)
->input(scandir(__DIR__))
->filter(function (string $path): bool {
return strpos($path, '.') !== 0;
})
->modify(function (string $path): array {
return findLinks(__DIR__ . '/' . $path);
})
->flatten()
->deduplicate()
->realize();
The key thing that Operation tries to do is make the execution flow more intuitive by making it read top-to-bottom. The PHP snippet above has the initial value (scandir __DIR__
) in the middle of the code. Not only must one read middle-out (rather than top-down), one must also jump up and down the page, increasingly more so, as more map and reduce calls are made. This is because the operation order isn’t consistent.
Furthermore, FP in PHP is held back by some engineering decisions, such as sort
and shuffle
taking a pointer. While this is great for performance, it prevents, for instance sort(range(1, 5))
.
With Operation
, a lot of this goes away:
sort
and shuffle
, making it easier to create longer chains of functional code.flatten
) included out of the box that would otherwise require you to write a reduce callback for that.If you need different behavior or new modifiers, Operation
is not a final
class and is rather easy to extend with your own constructions!
functional (MIT License) can be used with any framework and has no dependencies. This library may be installed via composer with the following command:
composer require ancarda/functional
For documentation, please run PHPDocumentor on src/
, or read the source code to see the DocBlocks.