Functional programming toolbox for PHP.

dev-master 2021-10-18 16:54 UTC

This package is auto-updated.

Last update: 2021-10-18 16:54:55 UTC


Latest Stable Version GitHub stars Total Downloads GitHub Workflow Status Scrutinizer code quality Type Coverage Code Coverage License Read the Docs Donate! Donate!

Functional Programming Toolbox


Functional Programming Toolbox (FPT for the friends) is a set of stateless and immutable helper classes to facilitate the use of functional programming(FP) concepts.

This projects doesn't aim to transform PHP into a full featured FP language, but it will helps users willing to use and understand a subset of FP concepts in their own code.


  • PHP 8


composer require loophp/fpt


Curry and Partial



namespace Example;

include __DIR__ . '/vendor/autoload.php';

use loophp\fpt\FPT;

// The "curry" method combine at the same time the
// Curry and Partial, see example below.
$explode = FPT::curry()('explode');

// Thanks to the new PHP 8 feature "named arguments",
// you can pass arguments in any order.

var_dump($explode(',', 'a,b,c'));
var_dump($explode(separator: ',', string: 'a,b,c'));
var_dump($explode(separator: ',')(string: 'a,b,c'));
var_dump($explode(string: 'a,b,c', separator: ','));
var_dump($explode(string: 'a,b,c')(separator: ','));

// All of these will return:
 * array(3) {
 *   [0] => string(1) "a"
 *   [1] => string(1) "b"
 *   [2] => string(1) "c"
 * }

// You can pass an optional "arity" argument

$explode = FPT::curry()('explode', 3);

var_dump($explode(',', 'a,b,c', 2));
var_dump($explode(separator: ',', string: 'a,b,c', limit: 2));
var_dump($explode(separator: ',')(string: 'a,b,c')(limit: 2));
var_dump($explode(limit: 2, string: 'a,b,c', separator: ','));
var_dump($explode(limit: 2)(string: 'a,b,c')(separator: ','));

// All of these will return:
 * array(2) {
 *   [0] => string(1) "a"
 *   [1] => string(3) "b,c"
 * }




namespace Example;

include __DIR__ . '/vendor/autoload.php';

use loophp\fpt\FPT;

$closure = static fn (string $first, string $second): string => sprintf("My cats names are %s and %s.", $first, $second);

$composedClosure = FPT::compose()('strtoupper', $closure);

$composedClosure('Izumi', 'Nakano'); // "MY CATS NAMES ARE IZUMI AND NAKANO."


The API will give you a pretty good idea of the existing methods and what you can do with it.

I'm doing my best to keep the documentation up to date, if you found something odd, please let me know in the issue queue.

Code quality, tests and benchmarks

Every time changes are introduced into the library, Github run the tests.

The library has tests written with PHPSpec. Feel free to check them out in the spec directory. Run composer phpspec to trigger the tests.

Before each commit some inspections are executed with GrumPHP, run composer grumphp to check manually.

The quality of the tests is tested with Infection a PHP Mutation testing framework, run composer infection to try it.

Static analysers are also controlling the code. PHPStan and PSalm are enabled to their maximum level.


Feel free to contribute by sending Github pull requests. I'm quite reactive :-)

If you can't contribute to the code, you can also sponsor me on Github or Paypal.


See CHANGELOG.md for a changelog based on git commits.

For more detailed changelogs, please check the release changelogs.


The API documentation has been inspired by the Ramda project.