{"id":15494146,"url":"https://github.com/farolfo/list-comprehensions-in-java","last_synced_at":"2025-04-22T20:23:23.932Z","repository":{"id":33705593,"uuid":"37358804","full_name":"farolfo/list-comprehensions-in-java","owner":"farolfo","description":"(college project, see jComprehension for the lib) Build sets in mathematical set-builder notation with Java, like { x * 2 | x E {1,2,3,4} ^ x is even }","archived":false,"fork":false,"pushed_at":"2020-11-09T18:37:45.000Z","size":540,"stargazers_count":9,"open_issues_count":1,"forks_count":3,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-03-29T18:36:29.223Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","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/farolfo.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}},"created_at":"2015-06-13T05:11:46.000Z","updated_at":"2022-08-26T07:21:49.000Z","dependencies_parsed_at":"2022-09-24T17:13:36.000Z","dependency_job_id":null,"html_url":"https://github.com/farolfo/list-comprehensions-in-java","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farolfo%2Flist-comprehensions-in-java","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farolfo%2Flist-comprehensions-in-java/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farolfo%2Flist-comprehensions-in-java/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/farolfo%2Flist-comprehensions-in-java/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/farolfo","download_url":"https://codeload.github.com/farolfo/list-comprehensions-in-java/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250316602,"owners_count":21410573,"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-10-02T08:11:42.834Z","updated_at":"2025-04-22T20:23:23.908Z","avatar_url":"https://github.com/farolfo.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"# List Comprehensions in Java\nBuild lists in mathematical [set-builder notation](https://en.wikipedia.org/wiki/Set-builder_notation) with Java, like { x * 2 | x E {1,2} ^ x is even }.\n\nUsed at: :D https://github.com/farolfo/JComprehension\n\n### Usage and motivation\n\nIn algebra we are able to define sets using set-builder notation, also known as set comprehension or list comprehension. For example, this computes the even numbers in a set of integers\n\n```java\n{ x | x E {1,2,3,4} ^ x is even }\n// gives {2,4}\n```\n\nwhich is read as _give me the set of all x such that x belongs to {1,2,3,4} and x is even_.\n\nBut this notation jumped out of algebra and today we have ways to do this in [many](https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(list_comprehension)) programming languages, such as Haskell or python.\n\nNow you can achieve this in Java\n\n```java\nPredicate\u003cInteger\u003e even = x -\u003e x % 2 == 0;\n\nList\u003cInteger\u003e evens = new ListComprehension\u003cInteger\u003e()\n        .suchThat(x -\u003e {\n            x.belongsTo(Arrays.asList(1, 2, 3, 4));\n            x.is(even);\n        });\n// evens = {2,4};\n```\n\nAnd if we want to transform the output expression in some way like\n\n```java\n{ x * 2 | x E {1,2,3,4} ^ x is even }\n// gives {4,8}\n```\n\nOur java code would look like\n\n```java\nList\u003cInteger\u003e duplicated = new ListComprehension\u003cInteger\u003e()\n        .giveMeAll((Integer x) -\u003e x * 2)\n        .suchThat(x -\u003e {\n            x.belongsTo(Arrays.asList(1, 2, 3, 4));\n            x.is(even);\n        });\n// duplicated = {4,8}\n```\n\n### Further reading\n\n* [Set-builder notation.](https://en.wikipedia.org/wiki/Set-builder_notation)\n* [How are programming languages supporting this today?](https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(list_comprehension))\n* [Haskell's list comprehension](http://learnyouahaskell.com/starting-out#im-a-list-comprehension)\n\n### License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarolfo%2Flist-comprehensions-in-java","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffarolfo%2Flist-comprehensions-in-java","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffarolfo%2Flist-comprehensions-in-java/lists"}