{"id":33984864,"url":"https://github.com/mpajunen/phamda","last_synced_at":"2025-12-13T04:38:59.126Z","repository":{"id":27674661,"uuid":"31160799","full_name":"mpajunen/phamda","owner":"mpajunen","description":"Auto-curried function library for PHP","archived":false,"fork":false,"pushed_at":"2023-09-28T04:42:35.000Z","size":379,"stargazers_count":18,"open_issues_count":2,"forks_count":1,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-09T10:03:05.467Z","etag":null,"topics":["curried-functions","curry","function-composition","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"Dev275x/m1-fall-17","license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mpajunen.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-22T11:22:08.000Z","updated_at":"2023-07-17T13:53:36.000Z","dependencies_parsed_at":"2022-09-01T00:11:36.975Z","dependency_job_id":null,"html_url":"https://github.com/mpajunen/phamda","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/mpajunen/phamda","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpajunen%2Fphamda","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpajunen%2Fphamda/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpajunen%2Fphamda/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpajunen%2Fphamda/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mpajunen","download_url":"https://codeload.github.com/mpajunen/phamda/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mpajunen%2Fphamda/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27700072,"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","status":"online","status_checked_at":"2025-12-13T02:00:09.769Z","response_time":147,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["curried-functions","curry","function-composition","php"],"created_at":"2025-12-13T04:38:58.597Z","updated_at":"2025-12-13T04:38:59.120Z","avatar_url":"https://github.com/mpajunen.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Phamda\n\nPhamda is an auto-curried function library for PHP, heavily inspired by the Javascript library\n[Ramda](http://ramdajs.com/). PHP 7.0+ or HHVM is required.\n\n\n## Installation\n\nUsing composer: `composer require phamda/phamda`\n\n\n## Documentation\n\nDocumentation [is available on Read the Docs](https://phamda.readthedocs.io).\n\n\n## Examples\n\nThese examples highlight the major features of Phamda. Basic usage examples can also be found on the\n[function list](https://phamda.readthedocs.io/en/latest/functions.html).\n\n\n### Currying\n\nNearly all of the functions use automatic partial application or **currying**. For example you can call the\n`filter` function with only the predicate callback and get a new function:\n\n```php\nuse Phamda\\Phamda as P;\n\n$isPositive   = function ($x) { return $x \u003e 0; };\n$list         = [5, 7, -3, 19, 0, 2];\n$getPositives = P::filter($isPositive);\n\n$getPositives($list) === [5, 7, 3 =\u003e 19, 5 =\u003e 2];\n```\n\nThe final result is the same as using two arguments directly. Of course this new function could now be used to filter\nother lists as well.\n\nIt's also possible to create new curried functions, including from native PHP functions. The `curry` function\ntakes a function and initial parameters and returns a new function:\n\n```php\n$replaceBad = P::curry('str_replace', 'bad', 'good');\n\n$replaceBad('bad day') === 'good day';\n$replaceBad('not bad') === 'not good';\n```\n\n\n### Composition\n\nPhamda functions are **composable**. The basic functions can be used to create new, more complex functions. There are\nalso several functions to help with function composition. For example the `compose` function takes multiple argument\nfunctions and returns a new function. Calling this new function applies the argument functions in succession:\n\n```php\n$double           = function ($x) { return $x * 2; };\n$addFive          = function ($x) { return $x + 5; };\n$addFiveAndDouble = P::compose($double, $addFive);\n\n$addFiveAndDouble(16) === 42;\n\n// Equivalent to calling $double($addFive(16));\n```\n\nOften the `pipe` function is a more natural way to compose functions. It is similar to `compose`, but the argument\nfunctions are applied in reverse order:\n\n```php\n$doubleAndAddFive = P::pipe($double, $addFive);\n\n$doubleAndAddFive(16) === 37;\n```\n\n\n### Parameter order\n\nWhen using functional techniques it's usually most convenient if data is the last parameter. Often native PHP and\nlibrary functions do not follow for this pattern. Phamda includes some tools to make it easier to use these functions\nfunctionally. The simplest is `flip`, it switches the order of the first two parameters:\n\n```php\n$pow   = function ($a, $b) { return $a ** $b; };\n$powOf = P::flip($pow);\n\n$pow(2, 8) === 256;\n$powOf(2, 8) === 64;\n```\n\n`twist` is somewhat more complicated and will return a new function where the original first parameter is now last:\n\n```php\n$redact = P::twist('substr_replace')('REDACTED', 5);\n\n$redact('foobarbaz') === 'foobaREDACTED';\n```\n\nUsing `twist` may not work well with variadic functions. This is where `twistN` can be useful. It requires an additional\nparameter to set the location of the replaced parameter.\n\nAll of these functions return curried functions.\n\n\n\n### Pipelines\n\nCombining these techniques allows the building of function pipelines. In this example they are applied to processing a\nlist of badly formatted product data:\n\n```php\n$products = [\n    ['category' =\u003e 'QDT', 'weight' =\u003e 65.8, 'price' =\u003e 293.5, 'number' =\u003e 15708],\n    ['number' =\u003e 59391, 'price' =\u003e 366.64, 'category' =\u003e 'NVG', 'weight' =\u003e 15.5],\n    ['category' =\u003e 'AWK', 'number' =\u003e 89634, 'price' =\u003e 341.92, 'weight' =\u003e 35],\n    ['price' =\u003e 271.8, 'weight' =\u003e 5.3, 'number' =\u003e 38718, 'category' =\u003e 'ETW'],\n    ['price' =\u003e 523.63, 'weight' =\u003e 67.9, 'number' =\u003e 75905, 'category' =\u003e 'YVM'],\n    ['price' =\u003e 650.31, 'weight' =\u003e 3.9, 'category' =\u003e 'XPA', 'number' =\u003e 46289],\n    ['category' =\u003e 'WGX', 'weight' =\u003e 75.5, 'number' =\u003e 26213, 'price' =\u003e 471.44],\n    ['category' =\u003e 'KCF', 'price' =\u003e 581.85, 'weight' =\u003e 31.9, 'number' =\u003e 48160],\n];\n\n$formatPrice = P::flip('number_format')(2);\n$process     = P::pipe(\n    P::filter( // Only include products that...\n        P::pipe(\n            P::prop('weight'), // ... weigh...\n            P::gt(50.0) // ... less than 50.0.\n        )\n    ),\n    P::map( // For each product...\n        P::pipe(\n            // ... drop the weight field and fix field order:\n            P::pick(['number', 'category', 'price']),\n            // ... and format the price:\n            P::evolve(['price' =\u003e $formatPrice])\n        )\n    ),\n    P::sortBy( // Sort the products by...\n        P::prop('number') // ... comparing product numbers.\n    )\n);\n\n$process($products) === [\n    ['number' =\u003e 38718, 'category' =\u003e 'ETW', 'price' =\u003e '271.80'],\n    ['number' =\u003e 46289, 'category' =\u003e 'XPA', 'price' =\u003e '650.31'],\n    ['number' =\u003e 48160, 'category' =\u003e 'KCF', 'price' =\u003e '581.85'],\n    ['number' =\u003e 59391, 'category' =\u003e 'NVG', 'price' =\u003e '366.64'],\n    ['number' =\u003e 89634, 'category' =\u003e 'AWK', 'price' =\u003e '341.92'],\n];\n```\n\n\n## License\n\nMIT license, see LICENSE file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpajunen%2Fphamda","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmpajunen%2Fphamda","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmpajunen%2Fphamda/lists"}