https://github.com/morrisonlevi/algorithm
Composable algorithms for PHP 7
https://github.com/morrisonlevi/algorithm
Last synced: about 1 year ago
JSON representation
Composable algorithms for PHP 7
- Host: GitHub
- URL: https://github.com/morrisonlevi/algorithm
- Owner: morrisonlevi
- Created: 2015-04-29T01:59:38.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2017-08-03T16:46:15.000Z (almost 9 years ago)
- Last Synced: 2025-03-02T10:48:18.774Z (over 1 year ago)
- Language: PHP
- Homepage:
- Size: 47.9 KB
- Stars: 46
- Watchers: 4
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Basic Algorithms for PHP
[](https://codeclimate.com/github/morrisonlevi/Algorithm) [](https://codeclimate.com/github/morrisonlevi/Algorithm/coverage)
PHP's built-in functions such as `array_map`, `array_filter` and `array_reduce` have a few issues:
- They only work with arrays
- They are eager instead of lazy
- They are cumbersome to compose
This repository provides definitions for common algorithms such as `map`, `filter`, and `reduce` with certain characteristics:
- They work with any type that can be used in a foreach loop
- They are lazy
- They are not (too) cumbersome to compose
## Building
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:
- `make` (or `make load.php`): builds `load.php`
- `make phar`: builds `morrisonlevi_algorithm.phar`
- `make check`: runs the phpunit test suite
There is a script registered in the `composer.json` that will build `load.php` if the composer autoloader gets built.
## Examples
This example does a basic `map`. Note that the function that does the mapping comes first and the input data comes second:
```php
2,
1 => 4,
2 => 6,
)
*/
```
This example chains a `filter`, `map` and `sum` together:
```php
0;
};
$mul2 = function($value) {
return $value * 2;
};
$algorithm = chain(
filter($odd),
map($mul2),
sum()
);
var_dump($algorithm([1,2,3])); // int(8)
```