{"id":13593281,"url":"https://github.com/ircmaxell/monad-php","last_synced_at":"2025-04-09T02:33:08.603Z","repository":{"id":9313330,"uuid":"11154596","full_name":"ircmaxell/monad-php","owner":"ircmaxell","description":"A simple Monad library for PHP","archived":false,"fork":false,"pushed_at":"2015-09-10T05:41:57.000Z","size":233,"stargazers_count":295,"open_issues_count":6,"forks_count":37,"subscribers_count":18,"default_branch":"master","last_synced_at":"2024-11-06T14:42:28.295Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/ircmaxell.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":"2013-07-03T15:40:02.000Z","updated_at":"2024-10-30T10:44:14.000Z","dependencies_parsed_at":"2022-07-31T15:38:56.343Z","dependency_job_id":null,"html_url":"https://github.com/ircmaxell/monad-php","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/ircmaxell%2Fmonad-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ircmaxell%2Fmonad-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ircmaxell%2Fmonad-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ircmaxell%2Fmonad-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ircmaxell","download_url":"https://codeload.github.com/ircmaxell/monad-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247965776,"owners_count":21025435,"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-08-01T16:01:18.715Z","updated_at":"2025-04-09T02:33:03.590Z","avatar_url":"https://github.com/ircmaxell.png","language":"PHP","funding_links":[],"categories":["PHP","杂项","目录","Miscellaneous","结构( Architectural )","Architectural Architectural"],"sub_categories":["数据结构和存储 Data Structure and Storage"],"readme":"MonadPHP\n========\n\nThis is a basic Monad library for PHP.\n\nUsage\n=====\n\nValues are \"wrapped\" in the monad via either the constructor: `new MonadPHP\\Identity($value)` or the `unit()` method on an existing instance: `$monad-\u003eunit($value);`\n\nFunctions can be called on the wrapped value using `bind()`:\n\n    use MonadPHP\\Identity;\n    $monad = new Identity(1);\n    $monad-\u003ebind(function($value) { var_dump($value); });\n    // Prints int(1)\n\nAll calls to bind return a new monad instance wrapping the return value of the function.\n\n     use MonadPHP\\Identity;\n    $monad = new Identity(1);\n    $monad-\u003ebind(function($value) {\n            return 2 * $value;\n        })-\u003ebind(function($value) { \n            var_dump($value); \n        });\n    // Prints int(2)\n\nAdditionally, \"extracting\" the raw value is supported as well (since this is PHP and not a pure functional language)...\n\n    use MonadPHP\\Identity;\n    $monad = new Identity(1);\n    var_dump($monad-\u003eextract());\n    // Prints int(1)\n\nMaybe Monad\n===========\n\nOne of the first useful monads, is the Maybe monad. The value here is that it will only call the callback provided to `bind()` if the value it wraps is not `null`.\n\n    use MonadPHP\\Maybe;\n    $monad = new Maybe(1);\n    $monad-\u003ebind(function($value) { var_dump($value); });\n    // prints int(1)\n\n    $monad = new Maybe(null);\n    $monad-\u003ebind(function($value) { var_dump($value); });\n    // prints nothing (callback never called)...\n\nThe included Chain monad does the same thing, but providing a short-cut implementation for objects:\n\n    use MonadPHP\\Chain;\n    $monad = new Chain($someChainableObject);\n    $obj = $monad-\u003ecall1()-\u003ecall2()-\u003enonExistantMethod()-\u003ecall4()-\u003eextract();\n    var_dump($obj);\n    // null\n\nThis can prevent errors when used with chaining...\n\nList Monad\n==========\n\nThis abstracts away the concept of a list of items (an array):\n\n    use MonadPHP\\ListMonad;\n    $monad = new ListMonad(array(1, 2, 3, 4));\n    $doubled = $monad-\u003ebind(function($value) { return 2 * $value; });\n    var_dump($doubled-\u003eextract());\n    // Prints array(2, 4, 6, 8)\n\nNote that the passed in function gets called once per value, so it only ever deals with a single element, never the entire array...\n\nIt also works with any `Traversable` object (like iterators, etc). Just be aware that returning the new monad that's wrapped will alwyas become an array...\n\nComposition\n===========\n\nThese Monads can be composed together to do some really useful things:\n\n    use MonadPHP\\ListMonad;\n    use MonadPHP\\Maybe;\n\n    $monad = new ListMonad(array(1, 2, 3, null, 4));\n    $newMonad = $monad-\u003ebind(function($value) { return new Maybe($value); });\n    $doubled = $newMonad-\u003ebind(function($value) { return 2 * $value; });\n    var_dump($doubled-\u003eextract());\n    // Prints array(2, 4, 6, null, 8)\n\nOr, what if you want to deal with multi-dimensional arrays?\n\n    use MonadPHP\\ListMonad;\n    $monad = new ListMonad(array(array(1, 2), array(3, 4), array(5, 6)));\n    $newMonad = $monad-\u003ebind(function($value) { return new ListMonad($value); });\n    $doubled = $newMonad-\u003ebind(function($value) { return 2 * $value; });\n    var_dump($doubled-\u003eextract());\n    // Prints array(array(2, 4), array(6, 8), array(10, 12))\n\nThere also exist helper constants on each of the monads to get a callback to the `unit` method:\n\n    $newMonad = $monad-\u003ebind(Maybe::unit);\n    // Does the same thing as above \n\nReal World Example\n==================\n\nImagine that you want to traverse a multi-dimensional array to create a list of values of a particular sub-key. For example:\n\n    $posts = array(\n        array(\"title\" =\u003e \"foo\", \"author\" =\u003e array(\"name\" =\u003e \"Bob\", \"email\" =\u003e \"bob@example.com\")),\n        array(\"title\" =\u003e \"bar\", \"author\" =\u003e array(\"name\" =\u003e \"Tom\", \"email\" =\u003e \"tom@example.com\")),\n        array(\"title\" =\u003e \"baz\"),\n        array(\"title\" =\u003e \"biz\", \"author\" =\u003e array(\"name\" =\u003e \"Mark\", \"email\" =\u003e \"mark@example.com\")),\n    );\n    \nWhat if we wanted to extract all author names from this data set. In traditional procedural programming, you'd likely have a number of loops and conditionals. With monads, it becomes quite simple.\n\nFirst, we define a function to return a particular index of an array:\n\n    function index($key) {\n        return function($array) use ($key) {\n            return isset($array[$key]) ? $array[$key] : null;\n        };\n    }\n    \nBasically, this just creates a callback which will return a particular array key if it exists. With this, we have everything we need to get the list of authors.\n\n    $postMonad = new MonadPHP\\ListMonad($posts);\n    $names = $postMonad\n        -\u003ebind(MonadPHP\\Maybe::unit)\n        -\u003ebind(index(\"author\"))\n        -\u003ebind(index(\"name\"))\n        -\u003eextract();\n        \nFollow through and see what happens!\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fircmaxell%2Fmonad-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fircmaxell%2Fmonad-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fircmaxell%2Fmonad-php/lists"}