{"id":19005320,"url":"https://github.com/f3ath/maybe-just-nothing","last_synced_at":"2025-04-22T19:02:36.332Z","repository":{"id":55035197,"uuid":"266438591","full_name":"f3ath/maybe-just-nothing","owner":"f3ath","description":"Yet another variation of the Maybe monad written in Dart.","archived":false,"fork":false,"pushed_at":"2023-04-12T03:49:13.000Z","size":44,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-17T10:27:02.503Z","etag":null,"topics":["dart","flutter","hacktoberfest","just","maybe","monad","nothing"],"latest_commit_sha":null,"homepage":"","language":"Dart","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/f3ath.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-05-23T23:47:50.000Z","updated_at":"2023-04-11T16:47:31.000Z","dependencies_parsed_at":"2024-11-08T18:31:55.628Z","dependency_job_id":"0be67e85-b222-4f13-a349-18885cb08f69","html_url":"https://github.com/f3ath/maybe-just-nothing","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f3ath%2Fmaybe-just-nothing","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f3ath%2Fmaybe-just-nothing/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f3ath%2Fmaybe-just-nothing/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/f3ath%2Fmaybe-just-nothing/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/f3ath","download_url":"https://codeload.github.com/f3ath/maybe-just-nothing/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250306609,"owners_count":21408925,"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":["dart","flutter","hacktoberfest","just","maybe","monad","nothing"],"created_at":"2024-11-08T18:27:06.492Z","updated_at":"2025-04-22T19:02:36.289Z","avatar_url":"https://github.com/f3ath.png","language":"Dart","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Maybe Just Nothing\nYet another variation of the good old Maybe monad with eager execution written in Dart. \n\n## Creating maybe-values\nInternally, `Maybe` is an abstract class with two implementations: `Just` and `Nothing`.\n\n\nThe most common scenario is probably discarding null from nullable values:\n```dart\nint? nullableValue;\nfinal val = Just(nullableValue).type\u003cint\u003e(); // val is either Just\u003cint\u003e or Nothing\u003cint\u003e\nfinal doubled = val.map((x) =\u003e x * 2);\ndoubled.ifPresent(print); // would print the doubled value of nullableValue if it's not null\n```\n\nNullable values can also be wrapped in `Just`:\n```dart\nint? nullableValue;\nfinal val = Just(nullableValue); // val is Just\u003cint?\u003e\nfinal justNull = Maybe(null); // creates Just\u003cvoid\u003e\n```\n\nThe `Nothing` value may be created either typed or untyped:\n```dart\nfinal nothing1 = Nothing(); // Nothing\u003cObject?\u003e\nfinal nothing2 = Nothing\u003cint\u003e(); // Nothing\u003cint\u003e\nfinal nothing3 = Nothing\u003cString?\u003e(); // Nothing\u003cString?\u003e\n```\n\nYou may even distinguish between presence and absence of `null`s themselves:\n```dart\nMaybe\u003cvoid\u003e yay = Just(null); // Just\u003cNull\u003e\nMaybe\u003cvoid\u003e nay = Nothing\u003cvoid\u003e(); // Nothing\u003cNull\u003e\n```\n\n## Mapping values\nMapping means transformation of the wrapped value by applying a function. \nSince `Maybe` itself is immutable, mapping operations do not actually modify the value.\nInstead, they always return another `Maybe`. \n```dart\nJust(2).map((x) =\u003e x * 2).ifPresent(print); // prints \"4\"\n```\n\nIf the mapping function also returns a `Maybe`, use `flatMap()`:\n```dart\nMaybe\u003cint\u003e triple(int x) =\u003e Just(x).map((x) =\u003e x * 3);\n\nJust(2).flatMap(triple).ifPresent(print); // prints \"6\"\n```\n\nAn operation on two maybe-values can be performed using `merge()`:\n```dart\nfinal two = Just(2);\nfinal three = Just(3);\n\ntwo.merge(three, (x, y) =\u003e x + y).ifPresent(print); // prints \"5\"\n```\n\n## Filtering values\nFiltering is checking whether the maybe-value satisfies a certain condition. If it does, \nthe value remains intact, otherwise `Nothing` is returned. \n\nTo filter by the value itself, use the `where()`: \n```dart\nJust(2).where((x) =\u003e x.isEven).ifPresent(print); // prints \"2\"\nJust(3).where((x) =\u003e x.isEven).ifPresent(print); // 3 is odd, so nothing happens\n```\n\nTo filter by type, use `type\u003cT\u003e()`:\n```dart\nfinal maybeInt = Just(2).type\u003cint\u003e(); // Just\u003cint\u003e\nfinal maybeString = Just(2).type\u003cString\u003e(); // Nothing\u003cString\u003e\n```\n\n## Fallback chain\nThe `chain()` method implements the [Chain of Responsibility] design pattern. It accepts another\nmaybe-value of the same type. If the current value is `Nothing`, the next value in the chain gets returned.\n\nAnother way to implement the same idea is to use the `fallback()` method. It accepts a \"fallback\" \nfunction which returns another maybe-value of the same type. If the current value is `Nothing`, \nthis fallback function will be called and its result will be returned. You can provide several fallback functions. \nThey will be called in sequence until a `Just` value is received.\n\n\n```dart\n\nNothing\u003cint\u003e()\n  .chain(Nothing\u003cint\u003e()) // this will be skipped\n  .chain(Just(2)) // this value is not empty, so it will be used\n  .chain(Just(3)) // this value will NOT be used\n  .ifPresent(print); // prints \"2\"\n\n// Same with fallback()\nNothing\u003cint\u003e()\n  .fallback(() =\u003e Nothing\u003cint\u003e()) // this result will be skipped\n  .fallback(() =\u003e Just(2)) // this function returns a non-empty value\n  .fallback(() =\u003e Just(3)) // this function will NOT be called\n  .ifPresent(print); // prints \"2\"\n```\n\n## Consuming the value\nThe intention of `Maybe` is to give it the consumer function instead of retrieving the value.\nThis is the most concise and clear way of using it.\n```dart\nMaybe a;\na\n  ..ifPresent(print)\n  ..ifNothing(() {/* do something else*/});\n```\n\n## Reading the value\nSometimes, however, you need the actual value. In such cases you'll have to provide the default value as well. \n\nIn the simplest scenario, use `or()`:\n\n```dart\nMaybe\u003cint\u003e a;\nfinal value = a.or(0); // value is 0\nfinal valueFromFuture = await a.orAsync(Future.value(0)); // value is 0\n```\n\nA provider function can be specified instead of the default value:\n\n```dart\nMaybe\u003cint\u003e a;\nfinal value = a.orGet(() =\u003e 0); // value is 0\nfinal valueFromFuture = await a.orGetAsync(() async =\u003e 0); // value is 0\n```\n\nIf there is no default value, an exception can be thrown:\n\n```dart\nMaybe\u003cint\u003e a;\nfinal value = a.orThrow(() =\u003e 'Oops!');\n```\n\nIn some rare cases, it can be convenient to check for emptiness directly:\n\n```dart\nMaybe\u003cint\u003e myInt;\n\nif (myInt is Just\u003cint\u003e) {\n  print(myInt.value); // .value is guaranteed to be non-null\n}\n\nif (myInt is Nothing) {\n  print('The value is missing');\n}\n```\n\n[Chain of Responsibility]: https://refactoring.guru/design-patterns/chain-of-responsibility","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff3ath%2Fmaybe-just-nothing","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ff3ath%2Fmaybe-just-nothing","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ff3ath%2Fmaybe-just-nothing/lists"}