dev-master
9999999-devComposable algorithms in PHP
The Requires
- php ^7.0
The Development Requires
dev-php7.1
dev-php7.1Composable algorithms in PHP
The Requires
- php ^7.1
The Development Requires
Wallogit.com
2017 © Pedro Peláez
Composable algorithms in PHP
PHP's built-in functions such as array_map, array_filter and array_reduce have a few issues:, (*2)
This repository provides definitions for common algorithms such as map, filter, and reduce with certain characteristics:, (*3)
PHP does not have function autoloading at the time of this writing. Since this project is mostly functions it uses a makefile to build load.php which will include all of the functions for use. You can also build a phar or run the unit tests:, (*4)
make (or make load.php): builds load.php
make phar: builds morrisonlevi_algorithm.phar
make check: runs the phpunit test suiteThere is a script registered in the composer.json that will build load.php if the composer autoloader gets built., (*5)
This example does a basic map. Note that the function that does the mapping comes first and the input data comes second:, (*6)
<?php
namespace morrisonlevi\Algorithm;
require __DIR__ . '/load.php';
$mul2 = function ($value) {
return $value * 2;
};
$result = map($mul2)([1,2,3]);
var_export(iterator_to_array($result));
/*
array (
0 => 2,
1 => 4,
2 => 6,
)
*/
This example chains a filter, map and sum together:, (*7)
<?php
namespace morrisonlevi\Algorithm;
require __DIR__ . '/load.php';
$odd = function($value) {
return $value % 2 > 0;
};
$mul2 = function($value) {
return $value * 2;
};
$algorithm = chain(
filter($odd),
map($mul2),
sum()
);
var_dump($algorithm([1,2,3])); // int(8)
Composable algorithms in PHP
Composable algorithms in PHP