{"id":15647043,"url":"https://github.com/morrisonlevi/algorithm","last_synced_at":"2025-04-30T12:38:55.633Z","repository":{"id":31205728,"uuid":"34766688","full_name":"morrisonlevi/Algorithm","owner":"morrisonlevi","description":"Composable algorithms for PHP 7","archived":false,"fork":false,"pushed_at":"2017-08-03T16:46:15.000Z","size":49,"stargazers_count":46,"open_issues_count":0,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-02T10:48:18.774Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/morrisonlevi.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-04-29T01:59:38.000Z","updated_at":"2022-11-03T11:30:48.000Z","dependencies_parsed_at":"2022-08-22T20:31:22.467Z","dependency_job_id":null,"html_url":"https://github.com/morrisonlevi/Algorithm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morrisonlevi%2FAlgorithm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morrisonlevi%2FAlgorithm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morrisonlevi%2FAlgorithm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/morrisonlevi%2FAlgorithm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/morrisonlevi","download_url":"https://codeload.github.com/morrisonlevi/Algorithm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242650901,"owners_count":20163610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-03T12:16:37.112Z","updated_at":"2025-03-09T05:31:11.905Z","avatar_url":"https://github.com/morrisonlevi.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Basic Algorithms for PHP\n\n[![Code Climate](https://codeclimate.com/github/morrisonlevi/Algorithm/badges/gpa.svg)](https://codeclimate.com/github/morrisonlevi/Algorithm) [![Test Coverage](https://codeclimate.com/github/morrisonlevi/Algorithm/badges/coverage.svg)](https://codeclimate.com/github/morrisonlevi/Algorithm/coverage)\n\nPHP's built-in functions such as `array_map`, `array_filter` and `array_reduce` have a few issues:\n\n  - They only work with arrays\n  - They are eager instead of lazy\n  - They are cumbersome to compose\n\nThis repository provides definitions for common algorithms such as `map`, `filter`, and `reduce` with certain characteristics:\n\n  - They work with any type that can be used in a foreach loop\n  - They are lazy\n  - They are not (too) cumbersome to compose\n\n## Building\n\nPHP 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:\n\n  - `make` (or `make load.php`): builds `load.php`\n  - `make phar`: builds `morrisonlevi_algorithm.phar`\n  - `make check`: runs the phpunit test suite\n\nThere is a script registered in the `composer.json` that will build `load.php` if the composer autoloader gets built.\n\n## Examples\n\nThis example does a basic `map`. Note that the function that does the mapping comes first and the input data comes second:\n```php\n\u003c?php\n\nnamespace morrisonlevi\\Algorithm;\n\nrequire __DIR__ . '/load.php';\n\n$mul2 = function ($value) {\n    return $value * 2;\n};\n\n$result = map($mul2)([1,2,3]);\n\nvar_export(iterator_to_array($result));\n/*\narray (\n  0 =\u003e 2,\n  1 =\u003e 4,\n  2 =\u003e 6,\n)\n*/\n```\n\nThis example chains a `filter`, `map` and `sum` together:\n\n```php\n\u003c?php\n\nnamespace morrisonlevi\\Algorithm;\n\nrequire __DIR__ . '/load.php';\n\n$odd = function($value) {\n   return $value % 2 \u003e 0;\n};\n\n$mul2 = function($value) {\n    return $value * 2;\n};\n\n$algorithm = chain(\n    filter($odd),\n    map($mul2),\n    sum()\n);\n\nvar_dump($algorithm([1,2,3])); // int(8)\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorrisonlevi%2Falgorithm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmorrisonlevi%2Falgorithm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmorrisonlevi%2Falgorithm/lists"}