{"id":13565361,"url":"https://github.com/jasongoodwin/better-java-monads","last_synced_at":"2025-05-16T03:05:01.474Z","repository":{"id":22069216,"uuid":"25398397","full_name":"jasongoodwin/better-java-monads","owner":"jasongoodwin","description":null,"archived":false,"fork":false,"pushed_at":"2024-12-03T20:07:18.000Z","size":1058,"stargazers_count":269,"open_issues_count":9,"forks_count":44,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-04-19T04:32:56.407Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jasongoodwin.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2014-10-18T13:42:10.000Z","updated_at":"2025-02-11T09:55:01.000Z","dependencies_parsed_at":"2025-01-01T03:10:25.303Z","dependency_job_id":null,"html_url":"https://github.com/jasongoodwin/better-java-monads","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasongoodwin%2Fbetter-java-monads","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasongoodwin%2Fbetter-java-monads/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasongoodwin%2Fbetter-java-monads/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jasongoodwin%2Fbetter-java-monads/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jasongoodwin","download_url":"https://codeload.github.com/jasongoodwin/better-java-monads/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254459087,"owners_count":22074605,"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-01T13:01:45.395Z","updated_at":"2025-05-16T03:04:56.466Z","avatar_url":"https://github.com/jasongoodwin.png","language":"Java","funding_links":[],"categories":["Java"],"sub_categories":[],"readme":"better-java8-monads\n==================\n\nThis library was built immediately after Java8 became GA to help fill in some blanks (Try, Futures.sequence)\nThere are some other libraries mentioned w/ this project on Stack Overflow: https://stackoverflow.com/questions/27787772/try-monad-in-java-8\n\nFeature Overview\n----------------\n\n*Try* - Optional exists to express nulls in types, but there is no way to express success/failure in types. Try to the rescue! The Try type is very similar to the Try in Scala's standard lib.\n\n*CompletableFuture.sequence* - If you have a list of futures, there is no obvious way to get a future of a list. This will come in handy!\n\nUsage\n=====\n\nImport into your project:\n\nSBT\n---\n\n    \"com.jason-goodwin\" % \"better-monads\" % \"0.4.0\"\n\nMaven\n-----\n\n    \u003cdependency\u003e\n\t    \u003cgroupId\u003ecom.jason-goodwin\u003c/groupId\u003e\n\t    \u003cartifactId\u003ebetter-monads\u003c/artifactId\u003e\n\t    \u003cversion\u003e0.4.0\u003c/version\u003e\n    \u003c/dependency\u003e\n\nTry\n===\n\nThe try monad was attributed to Twitter and placed into the Scala standard library.\nWhile both Scala and Haskell have a monad Either which has a left and a right type, \na Try is specifically of a type T on success or an exception on failure.\n\nUsage\n-----\n\nThe Try api is meant to be similar to the Optional type so has the same functions.\n- get() returns the held value or throws the thrown exception\n- getUnchecked() returns the held value or throws the thrown exception wrapped in a RuntimeException instance\n- map(x) maps the success value x to a new value and type or otherwise passes the Failure forward.\n- flatMap((x) -\u003e f(x)) maps the success value x to a new Try of f(x).\n- recover((t) -\u003e x) will return the success value of the Try in the success case or the value x in the failure case. Exposes the exception.\n- recoverWith((t) -\u003e f(x)) will return the success value of the Try in the success case or a new try of f(x) in the failure case. Exposes the exception.\n- filter((x) -\u003e isTrue(x)) - If Success, returns the same Success if the predicate succeeds, otherwise, returns a Failure with type NoSuchElementException.\n- onSuccess((x) -\u003e f(x)) execute some code on success - takes Consumer (eg requires no return value).\n- onFailure((x) -\u003e f(x)) execute some code on failure - takes Consumer (eg requires no return value).\n- orElse(x) will return the success value of the Try in success case or the value x in failure case.\n- orElseTry(f) will return the success value of the Try in success case or a new Try(f) in the failure case.\n- orElseThrow(() -\u003e throw new T) gets result or on failure will throw checked exception of type T\n- toOptional() will return Optional of success value of Try (if not null), otherwise it will return an empty Optional\n\nFutures\n=======\nThere is no sequence method in the Java8 library so I've provided one. You'll find the function Futures.sequence which will convert a `List\u003cCompletableFuture\u003cT\u003e\u003e` into a `CompletableFuture\u003cList\u003cT\u003e\u003e`. This is useful in the common use case of processing all of the elements of a list concurrently.\n\nUsage\n-----\nSimply call Futures.sequence on a `List\u003cCompletableFuture\u003cT\u003e\u003e` to get back a single future with the list of your items.\n\n    List\u003cInteger\u003e list = IntStream.range(0, 100).boxed().collect(Collectors.toList());\n        int size = list.size();\n        List\u003cCompletableFuture\u003cInteger\u003e\u003e futures = list\n                .stream()\n                .map(x -\u003e CompletableFuture.supplyAsync(() -\u003e x))\n                .collect(Collectors.toList());\n\n    CompletableFuture\u003cList\u003cInteger\u003e\u003e futureList = Futures.sequence(futures);\n\n\nTests\n=====\n\nSee the tests for examples of all functionality.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasongoodwin%2Fbetter-java-monads","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjasongoodwin%2Fbetter-java-monads","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjasongoodwin%2Fbetter-java-monads/lists"}