{"id":19139697,"url":"https://github.com/franzose/lemonad","last_synced_at":"2025-07-14T10:34:55.331Z","repository":{"id":62507103,"uuid":"167942788","full_name":"franzose/lemonad","owner":"franzose","description":"Yet another monads implementation written in PHP","archived":false,"fork":false,"pushed_at":"2019-02-01T01:35:53.000Z","size":43,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-22T18:50:45.626Z","etag":null,"topics":["algorithms","datastructures","maybe","maybe-monad","monad","monads","optional","php","try","try-monad"],"latest_commit_sha":null,"homepage":"","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/franzose.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2019-01-28T10:24:49.000Z","updated_at":"2020-11-25T23:54:44.000Z","dependencies_parsed_at":"2022-11-02T12:31:23.221Z","dependency_job_id":null,"html_url":"https://github.com/franzose/lemonad","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/franzose/lemonad","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Flemonad","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Flemonad/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Flemonad/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Flemonad/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/franzose","download_url":"https://codeload.github.com/franzose/lemonad/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/franzose%2Flemonad/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265281026,"owners_count":23739859,"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":["algorithms","datastructures","maybe","maybe-monad","monad","monads","optional","php","try","try-monad"],"created_at":"2024-11-09T07:14:46.470Z","updated_at":"2025-07-14T10:34:55.291Z","avatar_url":"https://github.com/franzose.png","language":"PHP","readme":"# Lemonad\nIt is a small repository containing implementations of some monads.\n\n1. [Optional](https://github.com/franzose/lemonad#optional)\n2. [Maybe](https://github.com/franzose/lemonad#maybe)\n3. [Try](https://github.com/franzose/lemonad#try)\n\n## Optional\n`Optional` is useful when you don‘t exactly sure if you‘re dealing with an empty value or some meaningful one. It provides a clean and expressive API.\n\n```php\n\u003c?php\ndeclare(strict_types=1);\n\nuse Lemonad\\Optional;\nuse function Lemonad\\optional;\n\n$empty = Optional::empty();\n$empty-\u003eisPresent(); // false\n$empty-\u003eisAbsent(); // true\n\n$optOf42 = Optional::of(42);\n$optOf42-\u003eisPresent(); // true\n$optOf42-\u003eisAbsent(); // false\n\n$null = optional(null);\n$null-\u003eequals(Optional::empty()); // true\n$null-\u003eisAbsent(); // true\n$null-\u003eisPresent(); // false\n\n$present = Optional::of(42)-\u003efilter(function (int $value) {\n    return 42 === $value;\n});\n\n$present-\u003eisPresent(); // true\n$present-\u003eisAbsent(); // false\n\n$absent = Optional::of(42)-\u003efilter(function (int $value) {\n    return 43 === $value;\n});\n\n$absent-\u003eisPresent(); // false\n$absent-\u003eisAbsent(); // true\n\noptional(42)-\u003emap(function (int $value) {\n    return $value + 1;\n})-\u003eget(); // 43\n\nOptional::of(42)-\u003eflatMap(function (int $value) {\n    return Optional::of($value + 1);\n})-\u003eget(); // 43\n\nOptional::of(42)-\u003eifPresent(function (int $value) {\n    echo $value;\n}); // should output \"42\"\n\nOptional::ofNullable(null)-\u003eifPresentOrElse(function (int $value) {\n    echo $value;\n}, function () {\n    echo '999';\n}); // should output \"999\"\n\nOptional::ofNullable(null)-\u003eor(function () {\n    return Optional::of(42);\n})-\u003eget(); // 42\n\nOptional::ofNullable(null)-\u003eorElse(42); // 42\n\nOptional::ofNullable(null)-\u003eorElseGet(function () {\n    return 42;\n}); // 42\n\nOptional::ofNullable(null)-\u003eorElseThrow(function () {\n    return new \\InvalidArgumentException('No! No! No!');\n}); // it will throw \\InvalidArgumentException exception\n```\n\n## Maybe\nHere are some examples of the Maybe monad:\n\n```php\n\u003c?php\ndeclare(strict_types=1);\n\nuse Lemonad\\Maybe;\nuse function Lemonad\\maybe;\n\n$unknown = Maybe::unknown(); // unknown forever\n$unknown-\u003eisKnown(); // false\n\n$known = Maybe::definitely(42);\n$known-\u003eisKnown(); // true\n\nMaybe::of(null)-\u003eisKnown(); // false\nMaybe::of(42)-\u003eisKnown(); // true\nMaybe::of(42)-\u003eor(999); // 42\nMaybe::of(42)-\u003eorElse(Maybe::definitely(999)); // same instance with value of 42\nMaybe::of(42)-\u003eto(function (int $value) {\n    return $value + 1;\n}); // Maybe with value of 43\n\nmaybe(42)-\u003equery(function (int $value) {\n    return 42 === $value;\n}); // Maybe with value of boolean true\n\nMaybe::of(42)-\u003equery(function (int $value) {\n    return 43 === $value;\n}); // Maybe with value of boolean false\n\nMaybe::of(null)-\u003eor(42); // 42\nMaybe::of(null)-\u003eorElse(Maybe::definitely(42)); // Maybe with value of 42\nMaybe::of(null)-\u003eto(function () {}); // always a new instance of 'unknown' Maybe\nMaybe::of(null)-\u003equery(function () {}); // always a new instance of 'unknown' Maybe\nMaybe::of(null)-\u003eequals(Maybe::unknown()); // equals is always false\n\necho Maybe::definitely('Brian')-\u003eto(function (string $name) {\n    return $name . ' Adams';\n}); // will output \"Brian Adams\"\n```\n\n## Try\nA Try represents a computation that may either throw an exception or return a value. As `try` is a reserved keyword in PHP and class names are case insensitive, I could not use `Try` for the class name, so I named it `LetsTry`.\n\n```php\n\u003c?php\ndeclare(strict_types=1);\n\nuse Lemonad\\LetsTry;\nuse function Lemonad\\lets_try;\nuse function Lemonad\\noop;\nuse RuntimeException;\n\nLetsTry::perform(function () {\n    return 42;\n})-\u003egetOrElse(noop()); // 42\n\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003egetOrElse(function () {\n    return 42;\n}); // 42\n\nLetsTry::successful(42)-\u003egetOrElse(noop()); // 42\nLetsTry::successful(null); // will throw Lemonad\\Exception\\NullValueException\n\nLetsTry::failure(new RuntimeException('Argh!'))\n    -\u003egetOrElse(function () {\n        return 42;\n    }); // 42\n\n// Successful, do mapping\nlets_try(function () {\n    return 42;\n})-\u003emap(function (int $value) {\n    return $value + 1;\n})-\u003egetOrElse(noop()); // 43\n\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003emap(function () {\n    // this callback will not be called,\n    // new instance of `Failure` will be returned instead\n});\n\n// Successful, do mapping\nlets_try(function () {\n    return 42;\n})-\u003eflatMap(function (int $value) {\n    return lets_try(function () use ($value) {\n        return $value + 1;\n    });\n})-\u003egetOrElse(noop()); // 43\n\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003eflatMap(function () {\n    // again, this callback will not be called,\n    // new instance of `Failure` will be returned instead\n});\n\n// There's nothing to recover,\n// so the callback will not be called\nlets_try(function () {\n    return 42;\n})-\u003erecover(function () {\n    return 43;\n})-\u003egetOrElse(noop()); // 42\n\n// Again, there's nothing to recover,\n// so the callback will not be called\nlets_try(function () {\n    return 42;\n})-\u003erecoverWith(function () {\n    return lets_try(function () {\n        return 43;\n    });\n})-\u003egetOrElse(noop()); // 42\n\n// Here, exception was thrown,\n// so we need to recover from that\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003erecover(function (RuntimeException $exception) {\n    return $exception-\u003egetMessage();\n}); // Argh!\n\n// Again, exception was thrown,\n// so we need to recover from that\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003erecoverWith(function (RuntimeException $exception) {\n    return lets_try(function () use ($exception) {\n        return $exception-\u003egetMessage();\n    });\n}); // Argh!\n\nlets_try(function () {\n    return 42;\n})-\u003eorElse(function () {\n    return lets_try(function () {\n        return 43;\n    });\n})-\u003egetOrElse(noop()); // 42 as the Try is `Success`\n\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003eorElse(function () {\n    return lets_try(function () {\n        return 43;\n    });\n})-\u003egetOrElse(noop()); // 43 as the Try was `Failure`\n\n// Will perform another Try\nlets_try(function () {\n    return 42;\n})-\u003efilterOrElse(\n    function (int $value) {\n        return 42 === $value;\n    },\n    function () {\n        return new RuntimeException('Argh!');\n    }\n);\n\n// Will throw provided RuntimeException,\n// as the predicate is unsatisfied\nlets_try(function () {\n    return 42;\n})-\u003efilterOrElse(\n    function (int $value) {\n        return 99 === $value;\n    },\n    function () {\n        return new RuntimeException('Argh!');\n    }\n);\n\n// Will just return another `Failure` Try\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003efilterOrElse(\n    function (int $value) {\n        return 99 === $value;\n    },\n    function () {\n        return new RuntimeException('Argh!');\n    }\n);\n\nlets_try(function () {\n    return 42;\n})-\u003efold(noop(), function (int $value) {\n    return $value + 1;\n}); // 43\n\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003efold(\n    function (RuntimeException $exception) {\n        return $exception-\u003egetMessage();\n    },\n    function (int $value) {\n        return $value + 1;\n    }\n); // Argh!\n\nlets_try(function () {\n    return 42;\n})-\u003etoOptional(); // Optional which contains value of 42\n\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003etoOptional(); // An empty Optional\n\nlets_try(function () {\n    return 42;\n})-\u003eforEach(function (int $value) {\n    // do something\n});\n\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003eforEach(function () {\n    // for `Failure`s it is a noop\n});\n\nlets_try(function () {\n    return 42;\n})-\u003eisSuccess(); // true\n\nlets_try(function () {\n    throw new RuntimeException('Argh!');\n})-\u003eisFailure(); // true\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffranzose%2Flemonad","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffranzose%2Flemonad","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffranzose%2Flemonad/lists"}