PHPackages                             morrisonlevi/algorithm - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. morrisonlevi/algorithm

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

morrisonlevi/algorithm
======================

Composable algorithms in PHP

4682PHP

Since Jan 6Pushed 8y ago4 watchersCompare

[ Source](https://github.com/morrisonlevi/Algorithm)[ Packagist](https://packagist.org/packages/morrisonlevi/algorithm)[ RSS](/packages/morrisonlevi-algorithm/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Basic Algorithms for PHP
========================

[](#basic-algorithms-for-php)

[![Code Climate](https://camo.githubusercontent.com/6593e95c96085096b6d5819b51636e76467f00a5dcd58b933b96c9fe4b18347e/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d6f727269736f6e6c6576692f416c676f726974686d2f6261646765732f6770612e737667)](https://codeclimate.com/github/morrisonlevi/Algorithm) [![Test Coverage](https://camo.githubusercontent.com/055763dd2b9888428d1767d516b84fa861b40c2c3fcf7dd8beae0891dcbd497d/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f6d6f727269736f6e6c6576692f416c676f726974686d2f6261646765732f636f7665726167652e737667)](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
--------

[](#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
--------

[](#examples)

This example does a basic `map`. Note that the function that does the mapping comes first and the input data comes second:

```
