{"id":17551041,"url":"https://github.com/mpetrovich/dash","last_synced_at":"2025-06-29T13:03:33.941Z","repository":{"id":34348002,"uuid":"38269264","full_name":"mpetrovich/dash","owner":"mpetrovich","description":"A functional programming library for PHP. Inspired by Underscore, Lodash, and Ramda.","archived":false,"fork":false,"pushed_at":"2023-02-01T22:50:17.000Z","size":1919,"stargazers_count":104,"open_issues_count":4,"forks_count":8,"subscribers_count":9,"default_branch":"main","last_synced_at":"2025-05-19T23:05:21.483Z","etag":null,"topics":["functional-programming","lodash","php","ramda","underscore"],"latest_commit_sha":null,"homepage":"https://github.com/mpetrovich/dash/blob/main/docs/Operations.md","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mpetrovich.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-29T20:21:31.000Z","updated_at":"2024-11-20T16:32:40.000Z","dependencies_parsed_at":"2023-02-17T12:00:18.813Z","dependency_job_id":null,"html_url":"https://github.com/mpetrovich/dash","commit_stats":null,"previous_names":[],"tags_count":34,"template":false,"template_full_name":null,"purl":"pkg:github/mpetrovich/dash","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpetrovich%2Fdash","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpetrovich%2Fdash/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpetrovich%2Fdash/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpetrovich%2Fdash/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpetrovich","download_url":"https://codeload.github.com/mpetrovich/dash/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpetrovich%2Fdash/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260851172,"owners_count":23072551,"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":["functional-programming","lodash","php","ramda","underscore"],"created_at":"2024-10-21T04:44:42.710Z","updated_at":"2025-06-29T13:03:33.876Z","avatar_url":"https://github.com/mpetrovich.png","language":"PHP","readme":"# Dash \u0026nbsp; [![Latest Stable Version](https://poser.pugx.org/mpetrovich/dash/version)](https://packagist.org/packages/mpetrovich/dash) [![Build Status](https://travis-ci.org/mpetrovich/dash.svg?branch=master)](https://travis-ci.org/mpetrovich/dash) [![codecov](https://codecov.io/gh/mpetrovich/dash/branch/master/graph/badge.svg)](https://codecov.io/gh/mpetrovich/dash)\n\n**A functional programming library for PHP.** Inspired by Underscore, Lodash, and Ramda.\n\n```php\n$avgMaleAge = Dash\\chain([\n\t['name' =\u003e 'John', 'age' =\u003e 12, 'gender' =\u003e 'male'],\n\t['name' =\u003e 'Jane', 'age' =\u003e 34, 'gender' =\u003e 'female'],\n\t['name' =\u003e 'Pete', 'age' =\u003e 23, 'gender' =\u003e 'male'],\n\t['name' =\u003e 'Mark', 'age' =\u003e 11, 'gender' =\u003e 'male'],\n\t['name' =\u003e 'Mary', 'age' =\u003e 42, 'gender' =\u003e 'female'],\n])\n-\u003efilter(['gender', 'male'])\n-\u003emap('age')\n-\u003eaverage()\n-\u003evalue();\n\necho \"Average male age is $avgMaleAge.\";\n```\n\n#### Jump to:\n\n-   [Functions / Operations](#operations)\n-   [Highlights](#highlights)\n-   [Why use Dash?](#why-use-dash)\n-   [Installation](#installation)\n-   [Usage](#usage)\n    -   [Standalone](#standalone)\n    -   [Chaining](#chaining)\n    -   [Supported data types](#supported-data-types)\n    -   [Currying](#currying)\n    -   [Lazy evaluation](#lazy-evaluation)\n    -   [Custom operations](#custom-operations)\n    -   [Tips](#tips)\n-   [Changelog](https://github.com/mpetrovich/dash/releases)\n-   [Roadmap](docs/Roadmap.md)\n-   [Contributing](CONTRIBUTING.md)\n\n## Operations\n\n[View full list of operations here](docs/Operations.md)\n\n## Highlights\n\n-   [Many data types supported](#supported-data-types): arrays, objects, generators ([coming soon](https://github.com/mpetrovich/dash/issues/3)), [`Traversable`](http://php.net/manual/en/class.traversable.php), [`DirectoryIterator`](http://php.net/manual/en/class.directoryiterator.php), and more\n-   [Chaining](#chaining)\n-   [Currying](#currying)\n-   [Lazy evaluation](#lazy-evaluation)\n-   [Custom operations](#custom-operations)\n-   Well-tested: Comprehensive tests with nearly 3,000 test cases and [100% code coverage](https://codecov.io/gh/mpetrovich/dash)\n\n## Why use Dash?\n\nPHP's built-in `array_*` functions are limited, difficult to compose, inconsistent, and don't work across many data types.\n\nFor instance, let's say we want to find the average age of males in this list:\n\n```php\n$people = [\n\t['name' =\u003e 'John', 'age' =\u003e 12, 'gender' =\u003e 'male'],\n\t['name' =\u003e 'Jane', 'age' =\u003e 34, 'gender' =\u003e 'female'],\n\t['name' =\u003e 'Pete', 'age' =\u003e 23, 'gender' =\u003e 'male'],\n\t['name' =\u003e 'Mark', 'age' =\u003e 11, 'gender' =\u003e 'male'],\n\t['name' =\u003e 'Mary', 'age' =\u003e 42, 'gender' =\u003e 'female'],\n];\n```\n\nUsing PHP's built-in in functions, we might write something like this:\n\n```php\n$males = array_filter($people, function ($person) {\n\treturn $person['gender'] === 'male';\n});\n$avgMaleAge = array_sum(array_column($males, 'age')) / count($males);\n```\n\nDash makes common data transformation operations simpler:\n\n```php\n$avgMaleAge = Dash\\chain($people)\n\t-\u003efilter(['gender', 'male'])\n\t-\u003emap('age')\n\t-\u003eaverage()\n\t-\u003evalue();\n```\n\nThis is just a tiny subset of what Dash can do. [**See the full list of operations here.**](docs/Operations.md)\n\n## Installation\n\nRequires PHP 7.4+\n\n```sh\ncomposer require mpetrovich/dash\n```\n\n## Usage\n\nDash operations are pure functions that can be used alone or chained together.\n\n### Standalone\n\nOperations can be called as namespaced functions:\n\n```php\nDash\\map([1, 2, 3], function ($n) { return $n * 2; });  // === [2, 4, 6]\n```\n\nor as static methods:\n\n```php\nuse Dash\\Dash;\n\nDash::map([1, 2, 3], function ($n) { return $n * 2; });  // === [2, 4, 6]\n```\n\n`Dash\\_` can also be used as an alias for `Dash\\Dash`:\n\n```php\nuse Dash\\_;\n\n_::map([1, 2, 3], function ($n) { return $n * 2; });  // === [2, 4, 6]\n```\n\n### Chaining\n\nMultiple operations can be chained in sequence using `chain()`. Call `value()` to return the final value.\n\n```php\n$result = Dash\\chain([1, 2, 3, 4, 5])\n\t-\u003efilter('Dash\\isOdd')\n\t-\u003emap(function ($n) { return $n * 2; })\n\t-\u003evalue();\n\n// $result === [2, 6, 10]\n```\n\nTo explicitly convert the value to an array or `stdClass`, use `arrayValue()` or `objectValue()` respectively:\n\n```php\n$result = Dash\\chain(['a' =\u003e 1, 'b' =\u003e 2, 'c' =\u003e 3])\n\t-\u003efilter('Dash\\isOdd')\n\t-\u003emapValues(function ($n) { return $n * 2; })\n\t-\u003eobjectValue();\n\n// $result === (object) ['a' =\u003e 2, 'c' =\u003e 6]\n```\n\nFor convenience, `Dash\\chain()` can be aliased to a global function using `addGlobalAlias()`. It only needs to be called once during your application bootstrap:\n\n```php\n// In your application bootstrap:\nDash::addGlobalAlias('__');\n\n// Elsewhere:\n$result = __([1, 2, 3, 4, 5])\n\t-\u003efilter('Dash\\isOdd')\n\t-\u003emap(function ($n) { return $n * 2; })\n\t-\u003evalue();\n```\n\nSometimes you don't need the return value of the chain. However, the chain isn't processed until `value()` is called. For semantic convenience, `run()` is also an alias for `value()`:\n\n```php\n$chain = Dash\\chain(range(1, 5))\n\t-\u003ereverse()\n\t-\u003eeach(function ($n) {\n\t\techo \"T-minus $n...\\n\";\n\t\tsleep(1);\n\t});\n\n// Nothing echoed yet\n\n$chain-\u003evalue();\n// or\n$chain-\u003erun();\n\n// Echoes each of the following lines 1 second apart:\n// T-minus 5...\n// T-minus 4...\n// T-minus 3...\n// T-minus 2...\n// T-minus 1...\n```\n\n### Supported data types\n\nDash can work with a wide variety of data types, including:\n\n-   arrays\n-   objects (eg. `stdClass`)\n-   generators ([still in development](https://github.com/mpetrovich/dash/issues/3))\n-   anything that implements the [`Traversable`](http://php.net/manual/en/class.traversable.php) interface\n-   [`DirectoryIterator`](http://php.net/manual/en/class.directoryiterator.php), which is also a `Traversable` but cannot normally be used with `iterator_to_array()` [due to a PHP bug](https://bugs.php.net/bug.php?id=49755). Dash works around this transparently.\n\n#### Examples\n\nWith an array:\n\n```php\nDash\\chain([1, 2, 3, 4])\n\t-\u003efilter('Dash\\isEven')\n\t-\u003emap(function ($value) {\n\t\treturn $value * 2;\n\t})\n\t-\u003evalue();\n// === [4, 8]\n```\n\nWith an object:\n\n```php\nDash\\chain((object) ['a' =\u003e 1, 'b' =\u003e 2, 'c' =\u003e 3, 'd' =\u003e 4])\n\t-\u003efilter('Dash\\isOdd')\n\t-\u003ekeys()\n\t-\u003ejoin(', ')\n\t-\u003evalue();\n// === 'a, c'\n```\n\nWith a `Traversable`:\n\n```php\nDash\\chain(new ArrayObject(['a' =\u003e 1, 'b' =\u003e 2, 'c' =\u003e 3, 'd' =\u003e 4]))\n\t-\u003epick(['b', 'c'])\n\t-\u003evalues()\n\t-\u003esum()\n\t-\u003evalue();\n// === 5\n```\n\nWith a `DirectoryIterator`:\n\n```php\n$iterator = new FilesystemIterator(__DIR__, FilesystemIterator::SKIP_DOTS);\n\n$filenames = Dash\\chain($iterator)\n\t-\u003ereject(function ($fileinfo) {\n\t\treturn $fileinfo-\u003eisDir();\n\t})\n\t-\u003emap(function ($fileinfo) {\n\t\treturn pathinfo($fileinfo)['filename'];\n\t})\n\t-\u003evalue();\n```\n\n### Currying\n\n[`curry()`](docs/Operations.md#curry) and related operations can be used to create curried functions from any callable:\n\n```php\nfunction listThree($a, $b, $c) {\n\treturn \"$a, $b, and $c\";\n}\n\n$listThree = Dash\\curry('listThree');\n$listTwo = $listThree('first');\n$listTwo('second', 'third');  // === 'first, second, and third'\n```\n\nMost Dash functions have a curried version that accepts input data as the last parameter instead of as the first. Curried versions are located in the `Dash\\Curry` namespace:\n\n```php\nDash\\chain([\n\t'a' =\u003e 3,\n\t'b' =\u003e '3',\n\t'c' =\u003e 3,\n\t'd' =\u003e 3.0\n])\n-\u003efilter(Dash\\Curry\\identical(3))\n-\u003evalue();\n// === ['a' =\u003e 3, 'c' =\u003e 3]\n```\n\nSimilarly, [`partial()`](docs/Operations.md#partial) and related operations can be used to create partially-applied functions:\n\n```php\n$greet = function ($greeting, $name) {\n\treturn \"$greeting, $name!\";\n};\n\n$sayHello = Dash\\partial($greet, 'Hello');\n$sayHowdy = Dash\\partial($greet, 'Howdy');\n\n$sayHello('Mark');  // === 'Hello, Mark!'\n$sayHowdy('Jane');  // === 'Howdy, Jane!'\n```\n\n### Lazy evaluation\n\nChained operations are not evaluated until `value()` or `run()` is called. Furthermore, the input data can be changed and evaluated multiple times using `with()`. This makes it simple to create reusable chains:\n\n```php\n$chain = Dash\\chain()\n\t-\u003efilter('Dash\\isOdd')\n\t-\u003emap(function ($n) { return $n * 2; });\n\n$chain-\u003ewith([1, 2, 3])-\u003evalue();  // === [2, 6]\n$chain-\u003ewith([4, 5, 6, 7])-\u003evalue();  // === [10, 14]\n```\n\nChains can also be cloned and extended:\n\n```php\n// …continued from above\n$clone = clone $chain;\n$clone-\u003emap(function ($n) { $n + 1; })\n$clone-\u003evalue();  // === [11, 15]\n\n// The original chain is untouched\n$chain-\u003evalue();  // === [10, 14]\n```\n\nWhen `value()` is called, the result is cached until the chain is modified or the input is changed using `with()`.\n\n### Custom operations\n\nCustom operations can be added, retrieved, and removed using `setCustom()`, `getCustom()`, and `unsetCustom()`, respectively. `Dash\\custom()` is also an alias for `Dash::getCustom()`:\n\n```php\nDash::setCustom('triple', function ($n) { return $n * 3; });\n\n// Standalone\nDash::triple(4);  // === 12\n\n// Chained\nDash\\chain([1, 2, 3])\n\t-\u003esum()\n\t-\u003etriple()\n\t-\u003evalue();  // === 18\n\n// As an argument\nDash\\chain([1, 2, 3])\n\t-\u003emap('Dash\\Dash::triple')\n\t-\u003evalue();  // === [3, 6, 9]\n\n// As an argument using the Dash::getCustom() method\nDash\\chain([1, 2, 3])\n\t-\u003emap(Dash::getCustom('triple'))\n\t-\u003evalue();  // === [3, 6, 9]\n\n// Using the Dash\\custom() operation\nDash\\chain([1, 2, 3])\n\t-\u003emap(Dash\\custom('triple'))\n\t-\u003evalue();  // === [3, 6, 9]\n\nDash::unsetCustom('triple');\n```\n\nWhen chained, the current input is passed as the first parameter to the custom operation:\n\n```php\nDash::setCustom('divide', function($numerator, $denominator) { return $numerator / $denominator; });\n\nDash\\chain(6)-\u003edivide(2)-\u003evalue();  // === 2\n```\n\n### Tips\n\nIf you find that Dash doesn't have an operation that you need, fear not. Custom logic can be added without giving up Dash chaining or other features. The simplest way to integrate missing operations is via the [`Dash\\thru()`](docs/Operations.md#thru) operation, which allows custom logic to modify and seamlessly pass through its results to the next step in the chain.\n\nFor example, suppose we want to use `array_change_key_case()` and keep the usual Dash chaining semantics. With `thru()`, it's simple:\n\n```php\n$result = Dash\\chain(['one' =\u003e 1, 'two' =\u003e 2, 'three' =\u003e 3])\n\t-\u003efilter('Dash\\isOdd')\n\t-\u003ethru(function($input) {\n\t\treturn array_change_key_case($input, CASE_UPPER);\n\t})\n\t-\u003ekeys()\n\t-\u003evalue();\n\n// $result === ['ONE', 'THREE']\n```\n\nAlternatively, if you find yourself needing to use `array_change_key_case()` often, it may be better to add a new custom operation:\n\n```php\nDash::setCustom('keyCase', function ($input, $case) {\n\treturn array_change_key_case($input, $case);\n});\n```\n\nwhich you can then use like any other chainable Dash method:\n\n```php\n$result = Dash\\chain(['one' =\u003e 1, 'two' =\u003e 2, 'three' =\u003e 3])\n\t-\u003efilter('Dash\\isOdd')\n\t-\u003ekeyCase(CASE_UPPER)\n\t-\u003ekeys()\n\t-\u003evalue();\n\n// $result === ['ONE', 'THREE']\n```\n\n### Feedback\n\nFound a bug or have a suggestion? Please [create a new GitHub issue](https://github.com/mpetrovich/dash/issues/new). We want your feedback!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpetrovich%2Fdash","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpetrovich%2Fdash","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpetrovich%2Fdash/lists"}