Refactoring from the Imperative to the Functional Style

This page was contributed by Venkat Subramaniam under the UPL

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

  1. Converting Simple Loops

    Converting Simple Imperative Loops to Functional Style.

  2. Converting Loops with Steps

    Converting Imperative Loops with Steps to Functional Style.

  3. Converting foreach with if

    Converting Imperative Iteration using foreach with if to Functional Style.

  4. Converting Iteration with transformation

    Converting Imperative Iteration with transformation to Functional Style.

  5. Converting Data Sources to Streams

    Converting Data Sources to Streams.

Back to Tutorial List