{"id":21326235,"url":"https://github.com/masylum/you-call-me-maybe","last_synced_at":"2025-03-15T23:44:37.965Z","repository":{"id":6080181,"uuid":"7306642","full_name":"masylum/you-call-me-maybe","owner":"masylum","description":"Painless maybe monad for ruby","archived":false,"fork":false,"pushed_at":"2012-12-24T12:46:24.000Z","size":116,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-01-22T12:32:40.979Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/masylum.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":"2012-12-24T11:59:13.000Z","updated_at":"2014-06-17T17:53:18.000Z","dependencies_parsed_at":"2022-08-30T16:33:37.259Z","dependency_job_id":null,"html_url":"https://github.com/masylum/you-call-me-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/masylum%2Fyou-call-me-maybe","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masylum%2Fyou-call-me-maybe/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masylum%2Fyou-call-me-maybe/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/masylum%2Fyou-call-me-maybe/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/masylum","download_url":"https://codeload.github.com/masylum/you-call-me-maybe/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243806041,"owners_count":20350775,"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-11-21T21:08:53.307Z","updated_at":"2025-03-15T23:44:37.936Z","avatar_url":"https://github.com/masylum.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# This is crazy! I just met you! Here is my number! =\u003e Exception\n\n```\nget(le_girl.telephone.number)\n```\n\nWhat happens if `le_girl` does not have a telephone? Your application crashes!\n\n`nil` exceptions are probably among the most common ones, and they appear\nbecause we don't handle properly uncertainity.\n\nYou probably are throwing defensive code to your `nil` exceptions but to be honest\nthis is patching the problem instead of preventing it\n\n```\n# this is lame, and you call yourself an engineer?\nis_fake = le_girl.telephone \u0026\u0026\n          le_girl.telephone.number \u0026\u0026\n          le_girl.telephone.number.length \u003c 9\n\n# better but not so clever\nis_fake = le_girl.try(:telephone).try(:number).try(:length) \u003c 9\n```\n\n## Monads to the rescue\n\nA monad is like a box that holds a value. We are all using them without knowing it.\nArrays are beautiful Monads that hold 0 or more results of a computation.\n\n```\nreal_numbers = le_club.girls.map(\u0026:telephone)\n                            .map(\u0026:number)\n                            .filter {|number| number.length \u003e 9}\n```\n\nThis example is really similar to the one we showed before but you have to notice that no\ndefensive coding is needed at all. Each function received an array and returns an array\nwhich may contain 0 or more values.\n\n## The Maybe monad\n\nWhenever you have a computation that returns 0 or 1 result you can use the `maybe` monad.\n\n```\nis_fake = Maybe.new(le_girl.telephone).number.length.get \u003e 9\n```\n\n# API\n\nYou call me maybe is slightly based on Data.Maybe from haskell.\n\n## Static methods\n\n### new (a -\u003e Maybe[a])\n\nCreates a new `Maybe` instance witha  given value\n\n```\nMaybe.new(le_girl.number) # =\u003e Just '666888999'\n```\n\n### from_a (Array[a] -\u003e Maybe[a])\n\nCreates a new `Maybe` instance from an array\n\n```\nMaybe.from_a([])            # =\u003e Nothing\nMaybe.from_a(['999666555']) # =\u003e Just '999666555'\n```\n\n### cat (Array[Maybe[a]] -\u003e Array[a])\n\nExtracts the values from an array of `Maybe` instances\n\n```\nMaybe.cat(le_club.girls.map(\u0026:maybe_number)) # =\u003e ['999666555', '666888999']\n```\n\n### map_maybe (Array[a] -\u003e (a -\u003e Maybe[b]) -\u003e Array[b])\n\nMaps a given block that returns `Maybe` instances and returns their values\n\n```\nMaybe.map_maybe(le_club.girls) {|girl| Maybe.new(girl.number)} # =\u003e ['999666555', '666888999']\n```\n\n## Instance  methods\n\n### method_missing (Maybe[a])\n\nReturns a `Maybe` instance with the result of the method call\n\n```\nMaybe.new(le_girl.telephone).number.length # =\u003e Just 3\n```\n\n### get (b -\u003e (a -\u003e b) -\u003e b)\n\nReturns the value\n\n```\nMaybe.new(le_girl.number).get              # =\u003e nil\nMaybe.new(le_girl.number).get('666333888') # =\u003e '666333888'\nMaybe.new(le_girl.number).get(0, \u0026:length) # =\u003e 3\n```\n\n### just? (Boolean)\n\nReturns wether the monad is an instance of `Just`\n```\nMaybe.new(le_girl.number).just? # =\u003e true\n```\n\n### nothing? (Boolean)\n\nReturns wether the monad is an instance of `Nothing`\n\n```\nMaybe.new(le_girl.number).nothing? # =\u003e false\n```\n\n### to_a (Array[a])\n\nConverts the `Maybe` instance into an array\n\n```\nMaybe.new(le_girl.number).to_a # =\u003e ['666888999']\n```\n\n### == (Maybe[a])\n\nCompares to `Maybe` instances\n\n```\nMaybe.new(le_girl.number) == Maybe.new(le_girl.number) # =\u003e true\nMaybe.new(le_girl.number) == Nothing.new               # =\u003e false\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasylum%2Fyou-call-me-maybe","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmasylum%2Fyou-call-me-maybe","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmasylum%2Fyou-call-me-maybe/lists"}