{"id":20372755,"url":"https://github.com/sidmishraw/visitors","last_synced_at":"2026-06-06T17:33:24.879Z","repository":{"id":86400835,"uuid":"116926898","full_name":"sidmishraw/Visitors","owner":"sidmishraw","description":"Algebraic Data Types and Pattern matching in Java.","archived":false,"fork":false,"pushed_at":"2018-01-10T08:01:23.000Z","size":60,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-15T06:48:11.593Z","etag":null,"topics":["adt","java8","monads"],"latest_commit_sha":null,"homepage":null,"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/sidmishraw.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,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-01-10T08:01:07.000Z","updated_at":"2018-01-10T08:01:48.000Z","dependencies_parsed_at":null,"dependency_job_id":"ceed99a1-2f86-4924-9d55-13c0ddf88dd5","html_url":"https://github.com/sidmishraw/Visitors","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/sidmishraw%2FVisitors","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidmishraw%2FVisitors/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidmishraw%2FVisitors/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sidmishraw%2FVisitors/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sidmishraw","download_url":"https://codeload.github.com/sidmishraw/Visitors/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241921838,"owners_count":20042763,"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":["adt","java8","monads"],"created_at":"2024-11-15T01:14:47.019Z","updated_at":"2026-06-06T17:33:19.996Z","avatar_url":"https://github.com/sidmishraw.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Pattern Matching in Java using Visitor Design Pattern\n\n\\- Sid\n\n## Motivation\n\nSimulate Haskell like Algebraic Data Types and pattern matching in Java. Follow it up with Monads.\n\n```haskell\ntest :: Maybe Int -\u003e Int\ntest x = case x of\n\tNothing -\u003e error \"Nothing\"\n\t(Just y) -\u003e y\n```\n\n```java\n/**\n * Just a funky eval method that checks the type of data.\n * Returns {@code true} for {@link Just} instance and {@code false} for {@link Nothing} instance.\n * \n * @param maybe\n *            The {@link Maybe} instance.\n * @return True or False depending on the concrete type.\n * @throws Exception\n */\n@SuppressWarnings(\"unchecked\")\npublic static \u003cT\u003e T test(Maybe\u003cT\u003e maybe) throws Exception {\n    //\n    // Pattern matching simulated using Visitor Pattern.\n    //\n    MaybeVisitor\u003cT\u003e mVisitor = new MaybeVisitor\u003cT\u003e() {\n        /**\n         * The value used for holding the values for the time-being.\n         */\n        private T value;\n        \n        @Override\n        public void visit(Nothing\u003cT\u003e nothing) {\n            //\n            // simulates Haskell's `Nothing -\u003e -- stuff --`\n            //\n            System.out.println(\"Visiting a Nothing instance\");\n            this.value = null;\n        }\n        \n        @Override\n        public void visit(Just\u003cT\u003e just) {\n            //\n            // simulates Haskell's `(Just e) -\u003e -- do stuff --`\n            //\n            System.out.println(\"Visiting a Just instance with value = \" + just.getTheValue());\n            this.value = just.getTheValue();\n        }\n        \n        @SuppressWarnings(\"unused\")\n        public T getValue() {\n            return this.value;\n        }\n    };\n    //\n    // simulates Haskell's `case t of`\n    //\n    maybe.accept(mVisitor);\n    return (T) mVisitor.getClass().getMethod(\"getValue\").invoke(mVisitor);\n}\n```\n\n## Verdict\n\n* Java version is very verbose. \n\n* Scala's case classes are very good for this scenario. I might move over to Scala to design Monads and Pattern matching DSLs.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsidmishraw%2Fvisitors","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsidmishraw%2Fvisitors","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsidmishraw%2Fvisitors/lists"}