Refactoring from the Imperative to the Functional Style
This page was contributed by Venkat Subramaniam under the UPLThis part of the tutorial helps you to learn the functional style equivalent of the imperative style code we often find. As you move forward in your projects, wherever it makes sense, you can change imperative style code to functional style code using the mappings you learn in this tutorial.
In this series we cover the following conversions from the imperative to the functional style:
Tutorial | Imperative Style | Functional Style Equivalent |
---|---|---|
Converting Simple Loops | for() |
range() or rangeClosed() |
Converting Loops with Steps | for(...i = i + ...) |
iterate() with takeWhile() |
Converting foreach with if | foreach(...) { if... } |
stream() with filter() |
Converting Iteration with transformation | foreach(...) { ...transformation... } |
stream() with map() |
Converting to Streams | File read operation |
Files.lines() |
-
Converting Simple Loops
Converting Simple Imperative Loops to Functional Style.
-
Converting Loops with Steps
Converting Imperative Loops with Steps to Functional Style.
-
Converting foreach with if
Converting Imperative Iteration using foreach with if to Functional Style.
-
Converting Iteration with transformation
Converting Imperative Iteration with transformation to Functional Style.
-
Converting Data Sources to Streams
Converting Data Sources to Streams.