2017 © Pedro Peláez
 

library functional

Functional Programming Helper Functions and Classes

image

ancarda/functional

Functional Programming Helper Functions and Classes

  • Tuesday, July 17, 2018
  • by ancarda
  • Repository
  • 3 Watchers
  • 2 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 3 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

ancarda/functional

Functional Programming Helper Functions and Classes, (*1)

Latest Stable Version Total Downloads License Build Status Coverage Status, (*2)

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., (*3)

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., (*4)

(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., (*5)

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))., (*6)

With Operation, a lot of this goes away:, (*7)

  • Modifiers are always operating on the previous computation, therefore most only take a single parameter, if that. It's easier to call them as you don't need to remember any parameter orders.
  • Modifiers for sort and shuffle, making it easier to create longer chains of functional code.
  • Common, useful constructions like array hierarchy flattening (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!, (*8)


functional (MIT License) can be used with any framework and has no dependencies. This library may be installed via composer with the following command:, (*9)

composer require ancarda/functional

For documentation, please run PHPDocumentor on src/, or read the source code to see the DocBlocks., (*10)

The Versions

17/07 2018

dev-master

9999999-dev

Functional Programming Helper Functions and Classes

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

immutable functional

17/07 2018

1.1

1.1.0.0

Functional Programming Helper Functions and Classes

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

immutable functional

05/06 2018

1.0

1.0.0.0

Functional Programming Helper Functions and Classes

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

immutable functional