{"id":23902904,"url":"https://github.com/mtumilowicz/java-streams-laziness-needs-pure-functions","last_synced_at":"2025-07-01T10:34:31.245Z","repository":{"id":110876552,"uuid":"157275672","full_name":"mtumilowicz/java-streams-laziness-needs-pure-functions","owner":"mtumilowicz","description":"Why java streams laziness needs pure functions.","archived":false,"fork":false,"pushed_at":"2018-11-12T21:31:01.000Z","size":56,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-06-14T18:08:18.810Z","etag":null,"topics":["java-streams","laziness","pure-function","stream","stream-processing","streams"],"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/mtumilowicz.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,"zenodo":null}},"created_at":"2018-11-12T20:55:58.000Z","updated_at":"2018-11-12T21:50:50.000Z","dependencies_parsed_at":"2023-03-13T13:47:41.149Z","dependency_job_id":null,"html_url":"https://github.com/mtumilowicz/java-streams-laziness-needs-pure-functions","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mtumilowicz/java-streams-laziness-needs-pure-functions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava-streams-laziness-needs-pure-functions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava-streams-laziness-needs-pure-functions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava-streams-laziness-needs-pure-functions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava-streams-laziness-needs-pure-functions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mtumilowicz","download_url":"https://codeload.github.com/mtumilowicz/java-streams-laziness-needs-pure-functions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mtumilowicz%2Fjava-streams-laziness-needs-pure-functions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262944582,"owners_count":23388793,"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":["java-streams","laziness","pure-function","stream","stream-processing","streams"],"created_at":"2025-01-04T22:51:07.617Z","updated_at":"2025-07-01T10:34:31.235Z","avatar_url":"https://github.com/mtumilowicz.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.com/mtumilowicz/java-streams-laziness-needs-pure-functions.svg?branch=master)](https://travis-ci.com/mtumilowicz/java-streams-laziness-needs-pure-functions)\n\n# java-streams-laziness-needs-pure-functions\nExample why java streams laziness needs pure functions.\n\n_Reference_: https://www.youtube.com/watch?v=8srdPlPfFIk  \n_Reference_: https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html\n\n# preface\n## streams laziness, intermediary vs terminal operations\nStream operations are divided into intermediate (Stream-producing) \noperations and terminal (value- or side-effect-producing) operations. \nIntermediate operations are always lazy.\n\nIntermediate operations return a new stream. They are always lazy; \nexecuting an intermediate operation such as `filter()` does not actually \nperform any filtering, but instead creates a new stream that, when \ntraversed, contains the elements of the initial stream that match the \ngiven predicate. Traversal of the pipeline source does not begin until \nthe terminal operation of the pipeline is executed.\n\nIntermediate operations are further divided into stateless and stateful \noperations. Stateless operations, such as filter and map, retain no \nstate from previously seen element when processing a new element -- \neach element can be processed independently of operations on other \nelements. Stateful operations, such as distinct and sorted, may \nincorporate state from previously seen elements when processing new \nelements.\n\n**Pipelines containing exclusively stateless intermediate operations can \nbe processed in a single pass, whether sequential or parallel, with \nminimal data buffering.**\n\nCouple of **terminal operations**:\n* `Optional\u003cT\u003e min/max(Comparator\u003c? super T\u003e comparator)`\n* `boolean allMatch/anyMatch/noneMatch (Predicate\u003c? super T\u003e predicate)`\n* `void forEach(Consumer\u003c? super T\u003e action)`\n* `Optional\u003cT\u003e reduce(BinaryOperator\u003cT\u003e accumulator)`\n* `T reduce(T identity, BinaryOperator\u003cT\u003e accumulator)`\n\nCouple of **intermediary  operations**:\n* `\u003cR\u003e Stream\u003cR\u003e map(Function\u003c? super T, ? extends R\u003e mapper)`\n* `Stream\u003cT\u003e sorted() // if T is not comparable – runtime exception`\n* `Stream\u003cT\u003e sorted(Comparator\u003c? super T\u003e comparator)`\n* `Stream\u003cT\u003e peek(Consumer\u003c? super T\u003e action)`\n\n## effectively final\nA non final local variable or method parameter whose value is never \nchanged after initialization is known as effectively final.\n\nVariables used in the **lambda expressions** must be final or \neffectively final.\n\n## pure functions\nFunction to be pure, has to follow two rules:\n* it does not change anything\n* it does not depend on anything that changes\n\n# project description\nWe provide simple example in `StreamLazinessTest`:\n```\nint[] arr = {0};\n\nIntStream integers = IntStream.iterate(0, integer -\u003e ++integer);\nIntStream integersWhile = integers.takeWhile(x -\u003e x \u003c arr[0]); // takeWhile is intermediate\n\narr[0]=10;\n\nlong count = integersWhile.count();\n\nassertThat(count, is(10L));\n```\n* `integersWhile` when consumed (before `arr[0]=10`) when consumed\nwould return `assertThat(count, is(0L));`, cause `x -\u003e x \u003c arr[0]`\nwould be false (`arr = {0}`) so after changing the value we have\nunexpected result (suppose that other thread change the value).\n\n# remark\nNote that java to a certain degree requires pure functions:\n```\nint value = 0;\n\nIntStream integers = IntStream.iterate(0, integer -\u003e ++integer);\nIntStream integersWhile = integers.takeWhile(x -\u003e x \u003c value); // will not compile, cause: Variable used in lambda expression should be final or effectively final\n\nvalue=10;\n\nlong count = integersWhile.count();\n\nassertThat(count, is(10L));\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtumilowicz%2Fjava-streams-laziness-needs-pure-functions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmtumilowicz%2Fjava-streams-laziness-needs-pure-functions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmtumilowicz%2Fjava-streams-laziness-needs-pure-functions/lists"}