{"id":19615009,"url":"https://github.com/ghurtchu/fluentry","last_synced_at":"2025-04-28T02:31:05.222Z","repository":{"id":59750287,"uuid":"537790349","full_name":"Ghurtchu/fluenTry","owner":"Ghurtchu","description":":policeman::oncoming_police_car: Manage unchecked exceptions functionally.","archived":false,"fork":false,"pushed_at":"2023-04-07T07:50:38.000Z","size":173,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-05T05:11:23.465Z","etag":null,"topics":["abstraction","composition","declarative-programming","functional-programming","java","monad","reflection","stack-safe"],"latest_commit_sha":null,"homepage":"","language":"Java","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/Ghurtchu.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,"governance":null}},"created_at":"2022-09-17T11:41:04.000Z","updated_at":"2023-04-06T10:46:58.000Z","dependencies_parsed_at":"2023-11-29T16:11:22.436Z","dependency_job_id":null,"html_url":"https://github.com/Ghurtchu/fluenTry","commit_stats":null,"previous_names":["scalevolvable/fluentry","ghurtchu/fluentry"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghurtchu%2FfluenTry","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghurtchu%2FfluenTry/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghurtchu%2FfluenTry/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ghurtchu%2FfluenTry/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ghurtchu","download_url":"https://codeload.github.com/Ghurtchu/fluenTry/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251237900,"owners_count":21557366,"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":["abstraction","composition","declarative-programming","functional-programming","java","monad","reflection","stack-safe"],"created_at":"2024-11-11T10:54:54.220Z","updated_at":"2025-04-28T02:31:04.936Z","avatar_url":"https://github.com/Ghurtchu.png","language":"Java","readme":"#### An attempt to port `scala.util.Try` to Java.\n\nThe main class is called `Try`.\n\n`Try` has a few overloaded static constructors for instantiating lazy computations which is called `Try.of()`.\n\n`Try` instance may be constructed via one of these five arguments:\n - `Runnable`\n - `Consumer\u003cT\u003e with arg`,\n - `Supplier\u003cT\u003e`,\n - `Function\u003cT, V\u003e with arg`,\n - `BiFunction\u003cT, U, V\u003e with two args`\n\n`Try` has two sub-classes: `Success\u003cT\u003e` which holds the successful value and `Failure` which holds the exception.\n\n`Try` may be used in different ways, so without further ado let's see the examples below:\n\nproblem description: try getting the element from the list, if it fails return stringified 0, or else return the stringified square of the success value.\n\nsolution with pure java:\n```java\n   public static String getAndThenDoubleAndThenStringify(List\u003cInteger\u003e list, int index) {\n       String result;\n       try {\n           int number = list.get(index);\n           result = String.valueOf(number * 2);\n       } catch (Exception e) {\n           result = \"0\";\n       }\n       return result;\n   }\n```\n\nsolution with `fluenTry`:\n```java\n   public static String getAndThenDoubleAndThenStringify(List\u003cInteger\u003e list, int index) {\n       return Try.of(list, index, List::get).map(n -\u003e n * 2).fold(String::valueOf, \"0\");\n   }\n```\n\nHere is a Failed computation which returns `Failure(ArithmeticException)` instead of blowing up the calling stack by throwing an exception and succeeds with pure value with the help of `fold` combinator:\n```java\n   double result = Try.of(() -\u003e Math.random() * 2 * 5)\n           .flatMap(n -\u003e Try.of(() -\u003e n + 1))\n           .flatMap(n -\u003e Try.of(() -\u003e 55 / 0)) // This is where the `Success` turns into `Failure`\n           .map(Math::sqrt)\n           .fold(Function.identity(), 0.0); // but with the help of `fold` we turn that into pure value\n```\n\nLet's see another example:\n\nproblem description: try to parse json string into `Person` instance, if succeeds return true or else return false.\n\nsolution with pure java:\n```java\n   public static Optional\u003cPerson\u003e parseJson(String json) {\n       var jsonParser = new JsonParser\u003cPerson\u003e();\n       Optional\u003cPerson\u003e result;\n       try {\n           result = Optional.of(jsonParser.fromJson(json));\n       } catch (JsonParsingException jpe) {\n           result = Optional.empty();\n       }\n       return result;\n   }\n```\n\nsolution with `fluenTry` - takes a string argument and the `Function\u003cT, V\u003e` instance as a second argument, finally calles `toOptional()` which converts the `Try\u003cT\u003e` into `Optional\u003cT\u003e`:\n```java\n   public static Optional\u003cPerson\u003e parseJson(String json) {\n       var parser = new JsonParser\u003cPerson\u003e();\n       return Try.of(json, parser::fromJson).toOptional();\n   }\n```\n\nLet's see something related to java Enums:\n\nproblem description: Write a method on Weekend enum which returns true of the passed string is a Weekend day or else return false.\n\nsolution with pure java:\n```java\n    public enum Weekend {\n    \n        SATURDAY,\n        SUNDAY;\n\n        public static boolean isWeekend(String day) {\n            try {\n                String normalized = day.trim().toUpperCase();\n                Weekend.valueOf(normalized);\n                return true;\n            } catch (IllegalArgumentException iae) {\n                return false;\n            }\n        }\n\n   }\n```\n\nwith `fluenTry:` - takes a `Supplier\u003cString\u003e` as an argument, then `maps` and finally `folds` to either `true` or `false`:\n```java\n    public enum Weekend {\n    \n        SATURDAY,\n        SUNDAY;\n\n        public static boolean isWeekend(String day) {\n            return Try.of(() -\u003e day.trim().toUpperCase()).map(Weekend::valueOf).fold(d -\u003e true, false);\n        }\n   }\n```   \n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghurtchu%2Ffluentry","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fghurtchu%2Ffluentry","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fghurtchu%2Ffluentry/lists"}