{"id":13905516,"url":"https://github.com/tomstuart/monads","last_synced_at":"2025-06-22T21:05:33.190Z","repository":{"id":19309243,"uuid":"22547049","full_name":"tomstuart/monads","owner":"tomstuart","description":"Simple Ruby implementations of some common monads.","archived":false,"fork":false,"pushed_at":"2019-09-08T09:18:50.000Z","size":33,"stargazers_count":609,"open_issues_count":4,"forks_count":95,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-06-06T09:17:28.139Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://tomstu.art/refactoring-ruby-with-monads","language":"Ruby","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/tomstuart.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-02T13:52:07.000Z","updated_at":"2025-06-05T18:06:27.000Z","dependencies_parsed_at":"2022-08-20T23:40:26.050Z","dependency_job_id":null,"html_url":"https://github.com/tomstuart/monads","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/tomstuart/monads","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomstuart%2Fmonads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomstuart%2Fmonads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomstuart%2Fmonads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomstuart%2Fmonads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tomstuart","download_url":"https://codeload.github.com/tomstuart/monads/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tomstuart%2Fmonads/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261367503,"owners_count":23147851,"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-06T23:01:17.616Z","updated_at":"2025-06-22T21:05:28.177Z","avatar_url":"https://github.com/tomstuart.png","language":"Ruby","readme":"# Monads\n\n![](https://github.com/tomstuart/monads/workflows/Ruby/badge.svg)\n\nThis library provides simple Ruby implementations of some common [monads](http://en.wikipedia.org/wiki/Monad_(functional_programming)).\n\nThe most important method of each implementation is `#and_then` (a.k.a. `bind` or `\u003e\u003e=`), which is used to connect together a sequence of operations involving the value(s) inside the monad. Each monad also has a `.from_value` class method (a.k.a. `return` or `unit`) for constructing an instance of that monad from an arbitrary value.\n\n## `Optional`\n\n(a.k.a. the [maybe monad](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#The_Maybe_monad))\n\nAn `Optional` object contains a value that might be `nil`.\n\n```irb\n\u003e\u003e require 'monads/optional'\n=\u003e true\n\n\u003e\u003e include Monads\n=\u003e Object\n\n\u003e\u003e optional_string = Optional.new('hello world')\n=\u003e #\u003cstruct Monads::Optional value=\"hello world\"\u003e\n\n\u003e\u003e optional_result = optional_string.and_then { |string| Optional.new(string.upcase) }\n=\u003e #\u003cstruct Monads::Optional value=\"HELLO WORLD\"\u003e\n\n\u003e\u003e optional_result.value\n=\u003e \"HELLO WORLD\"\n\n\u003e\u003e optional_string = Optional.new(nil)\n=\u003e #\u003cstruct Monads::Optional value=nil\u003e\n\n\u003e\u003e optional_result = optional_string.and_then { |string| Optional.new(string.upcase) }\n=\u003e #\u003cstruct Monads::Optional value=nil\u003e\n\n\u003e\u003e optional_result.value\n=\u003e nil\n```\n\n## `Many`\n\n(a.k.a. the [list monad](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#Collections))\n\nA `Many` object contains multiple values.\n\n```irb\n\u003e\u003e require 'monads/many'\n=\u003e true\n\n\u003e\u003e include Monads\n=\u003e Object\n\n\u003e\u003e many_strings = Many.new(['hello world', 'goodbye world'])\n=\u003e #\u003cstruct Monads::Many values=[\"hello world\", \"goodbye world\"]\u003e\n\n\u003e\u003e many_results = many_strings.and_then { |string| Many.new(string.split(/ /)) }\n=\u003e #\u003cstruct Monads::Many values=[\"hello\", \"world\", \"goodbye\", \"world\"]\u003e\n\n\u003e\u003e many_results.values\n=\u003e [\"hello\", \"world\", \"goodbye\", \"world\"]\n```\n\n## `Eventually`\n\n(a.k.a. the [continuation monad](http://en.wikipedia.org/wiki/Monad_%28functional_programming%29#Continuation_monad))\n\nAn `Eventually` object contains a value that will eventually be available, perhaps as the result of an asynchronous process (e.g. a network request).\n\n```irb\n\u003e\u003e require 'monads/eventually'\n=\u003e true\n\n\u003e\u003e include Monads\n=\u003e Object\n\n\u003e\u003e eventually_string = Eventually.new do |success|\n     Thread.new do\n       sleep 5\n       success.call('hello world')\n     end\n   end\n=\u003e #\u003cstruct Monads::Eventually block=#\u003cProc\u003e\u003e\n\n\u003e\u003e eventually_result = eventually_string.and_then do |string|\n     Eventually.new do |success|\n       Thread.new do\n         sleep 5\n         success.call(string.upcase)\n       end\n     end\n   end\n=\u003e #\u003cstruct Monads::Eventually block=#\u003cProc\u003e\u003e\n\n\u003e\u003e eventually_result.run { |string| puts string }\n=\u003e #\u003cThread run\u003e\nHELLO WORLD\n```\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomstuart%2Fmonads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftomstuart%2Fmonads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftomstuart%2Fmonads/lists"}