{"id":20595581,"url":"https://github.com/johnpaulada/reason-maybe","last_synced_at":"2025-04-14T23:43:46.212Z","repository":{"id":57348973,"uuid":"139677228","full_name":"johnpaulada/reason-maybe","owner":"johnpaulada","description":"A simple Maybe library in Reason.","archived":false,"fork":false,"pushed_at":"2018-07-04T12:12:14.000Z","size":13,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-03T02:37:56.006Z","etag":null,"topics":["library","maybe","monad","reasonml"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/reason-maybe","language":"OCaml","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/johnpaulada.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":"2018-07-04T06:20:02.000Z","updated_at":"2019-01-22T23:34:20.000Z","dependencies_parsed_at":"2022-09-17T23:11:34.444Z","dependency_job_id":null,"html_url":"https://github.com/johnpaulada/reason-maybe","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/johnpaulada%2Freason-maybe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnpaulada%2Freason-maybe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnpaulada%2Freason-maybe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/johnpaulada%2Freason-maybe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/johnpaulada","download_url":"https://codeload.github.com/johnpaulada/reason-maybe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248788940,"owners_count":21161727,"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":["library","maybe","monad","reasonml"],"created_at":"2024-11-16T08:13:33.992Z","updated_at":"2025-04-14T23:43:46.191Z","avatar_url":"https://github.com/johnpaulada.png","language":"OCaml","funding_links":[],"categories":[],"sub_categories":[],"readme":"# reason-maybe\nA simple Maybe library in Reason.\n\n[![forthebadge](https://forthebadge.com/images/badges/gluten-free.svg)](https://forthebadge.com)\n[![forthebadge](https://forthebadge.com/images/badges/built-with-love.svg)](https://forthebadge.com)\n\n[![Made with Reason](https://img.shields.io/badge/Made%20with-Reason-red.svg?longCache=true\u0026style=for-the-badge)](https://reasonml.github.io/)\n\n## Installation\n- Install [Yarn](https://yarnpkg.com).\n- Run `yarn add reason-maybe`.\n- Add `reason-maybe` to your `bs-dependencies` in `bsconfig.json`\n\n**`bsconfig.json`**\n```json\n\"bs-dependencies\": [\n  \"reason-maybe\"\n]\n```\n\n## Usage\n\n### `from`\nTo create a Maybe use `Maybe.from`:\n```reason\nlet one = Maybe.from(Some(1)); /* Just(1) */\n```\n\n### `map`\nTo map a function on the Maybe, use `Maybe.map`:\n```reason\nlet plusOne = x =\u003e x + 1;\nlet onePlusOne = one |\u003e Maybe.map(plusOne); /* Just(2) */\n```\n\nAlternatively, you can import the equivalent infix operator `||\u003e`:\n```reason\nlet (||\u003e) = Maybe.(||\u003e);\n\nlet onePlusOne = one ||\u003e plusOne; /* Just(2) */\n```\n\nIf the map operation provides a value, it will return `Just(value)`. Else, it will return a `Nothing`.\n\n### `value`\nTo extract the value of the Maybe, we need to use `Maybe.value`:\n```reason\nlet value = onePlusOne |\u003e Maybe.value(0); /* 2 */\n```\n\nAlternatively, you can import the equivalent infix operator `\u003e|`:\n```reason\nlet (\u003e|) = Maybe.(\u003e|);\n\nlet value = onePlusOne \u003e| 0; /* 2 */\n```\n\nIf the Maybe was actually a `Nothing`, the value given to the `Maybe.value` function will be the resulting value.\nIn this case if `onePlusOne` was `Nothing`, the value of `value` would be `0`.\n\n### `chain`\nTo run a function that returns a Maybe on a Maybe, we use `Maybe.chain`:\n```reason\nlet two = Maybe.from(Some(2));\nlet plusOneMaybe = x =\u003e one |\u003e Maybe.map(y =\u003e x + y);\nlet twoPlusOneMaybe = two |\u003e Maybe.chain(plusOneMaybe); /* Just(3) */\n```\n\nAlternatively, you can import the equivalent infix operator `|||\u003e`:\n```reason\nlet (|||\u003e) = Maybe.(|||\u003e);\n\nlet twoPlusOneMaybe = two |||\u003e plusOneMaybe; /* Just(3) */\n```\n\nWe use the `plusOneMaybe` function which returns a Maybe on the `two` Maybe.\nInstead of `Maybe.map`, we use `Maybe.chain` to lift it from the returned Maybe unto the current Maybe.\n\n### `branch`\nIf the Maybe has turned into a Nothing and you want to handle that, you can use `Maybe.branch`:\n```reason\nlet addToRoute = s =\u003e x =\u003e\n  switch s {\n    | \"\" =\u003e Maybe.Nothing\n    | v =\u003e Maybe.Just(x ++ v)\n  }\n;\nlet noslug = () =\u003e \"/not_found\";\nlet hasslug = v =\u003e v\nlet getArticleRoute = slug =\u003e Maybe.from(Some(\"/articles/\"))\n  |\u003e Maybe.chain(addToRoute(slug))\n  |\u003e Maybe.branch(noslug, hasslug)\n  |\u003e Maybe.value(\"/articles\");\n\nlet validArticleRoute = getArticleRoute(\"awesome\");\nJs.log(validArticleRoute); /* /articles/awesome */\n\nlet invalidArticleRoute = getArticleRoute(\"\");\nJs.log(invalidArticleRoute); /* /not_found */\n```\n\nIf the Maybe is a `Just`, the `hasslug` method will be called. If the Maybe is a `Nothing`, the `noslug` will be called.\n\nIf want it to be less verbose, you can import and use the equivalent infix operator `\u003c-\u003e`:\n```reason\nlet (\u003c-\u003e) = Maybe.(\u003c-\u003e);\nlet (|||\u003e) = Maybe.(|||\u003e);\nlet (\u003e|) = Maybe.(\u003e|);\n\n/* Previously defined functions here */\n\nlet getArticleRoute = slug =\u003e\n    Maybe.from(Some(\"/articles/\"))\n        |||\u003e addToRoute(slug)\n        |\u003e (noslug \u003c-\u003e hasslug)\n        \u003e| \"/articles\"\n;\n\nlet validArticleRoute = getArticleRoute(\"awesome\");\nJs.log(validArticleRoute); /* /articles/awesome */\n\nlet invalidArticleRoute = getArticleRoute(\"\");\nJs.log(invalidArticleRoute); /* /not_found */\n```\n\nThis form is less readable but more terse yields the same result.\n\n## Development\n\n1. Clone this repo.\n2. Move to this directory with `cd reason-maybe`.\n3. Install [Yarn](https://yarnpkg.com).\n4. Run `yarn`.\n\n### Build\n```\nnpm run build\n```\n\n### Build + Watch\n\n```\nnpm run start\n```\n\n## License\nMIT","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnpaulada%2Freason-maybe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjohnpaulada%2Freason-maybe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjohnpaulada%2Freason-maybe/lists"}