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() |