GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing chrisdone.com — Stream fusion and composability (Java 8 and Haskell) for newbies Stream fusion and composability (Java 8 and Haskell) for newbiesNote: An LLM was not used in writing this article. In an online discussion, when Java 8 released their stream API, written about here, you can write e.g. Someone asked, “But my question: would the streams be faster than loops? Or is the only benefit better readability?” Someone answered that the benefit is that streams compose and loops don’t. What does composable mean here? Below is my answer, using two languages I know, JavaScript and Haskell. Composable in this context means: To be able to compose two things
into one without redundancy or overhead. For example, consider you want
to map a function If you want to filter the array based on a predicate Or maybe you want to take all elements until a a predicate
Now, if you want to do that all in one process you have a few options:
An ideal stream API will give you the last point, but be able to understand concepts like mapping and filtering and know how to merge them together into an efficient loop. This is called stream fusion, which you can google if you want to know more. I don’t know Java but I can give a Haskell example: (Note: In Haskell the operations separated by If I compile this with GHC, e.g. and look at the reduced output called Core, a language the compiler
generates code for before generating assembly or byte code, the
Which is pretty nifty. Furthermore, if you fold (also called reducing) e.g. Then that whole thing is also compiled into one loop: There’re limits to what can compose with what, though. |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |