Skip to content

Commit 50826e9

Browse files
authored
Merge pull request #61 from java/refactoring
next article in the series
2 parents f656822 + e334f07 commit 50826e9

File tree

5 files changed

+120
-3
lines changed

5 files changed

+120
-3
lines changed

app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/00_refactoring.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ In this series we cover the following conversions from the imperative to the fun
2222
| [Converting Simple Loops](id:refactoring.simple.loops) | `for()` | `range()` or `rangeClosed()` |
2323
| [Converting Loops with Steps](id:refactoring.loops.withsteps) | `for(...i = i + ...)` | `iterate()` with `takeWhile()` |
2424
| [Converting foreach with if](id:refactoring.foreach.withif) | `foreach(...) { if... }` | `stream()` with `filter()` |
25+
| [Converting Iteration with transformation](id:refactoring.iteration.withtransformation) | `foreach(...) { ...transformation... }` | `stream()` with `map()` |
2526

app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/01_converting_simple_loops.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The older versions of Java supported the Object-Oriented paradigm mixed with the
2323

2424
Imperative style is where we tell what to do and also how to do it. Functional style is declarative in nature, where we tell what to do and delegate the how or the details to the underlying libraries. Imperative style code may be easier to write since most of us are very familiar with it. However, the code becomes verbose, complex, and hard to read. Functional style may be hard at first, mainly because most programmers are less familiar with it. In general, it's easier to read, understand, and change. With practice, it becomes easier to write as well.
2525

26-
In this tutorial series we will take a look at a number of common imperative style code and find a mapping or an equivalent functional style code that we can use instead. As you work with your code based, when you're ready to fix a bug or make an enhancement, you may find it useful to refactor some of the imperative style code to the functional style. You can use this tutorial as a guide to find the imperative to functional style mappings for some common situations.
26+
In this [tutorial series](id:refactoring) we will take a look at a number of common imperative style code and find a mapping or an equivalent functional style code that we can use instead. As you work with your code based, when you're ready to fix a bug or make an enhancement, you may find it useful to refactor some of the imperative style code to the functional style. You can use this tutorial as a guide to find the imperative to functional style mappings for some common situations.
2727

2828
In this tutorial we'll focus on simple loops.
2929

app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/02_converting_loops_with_steps.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ author: ["VenkatSubramaniam"]
2020
<a id="steps">&nbsp;</a>
2121
## Iterating with Steps
2222

23-
In the previous article in this series we looked at converting simple loops written in the imperative style to the functional style. In this article we'll see how to take on loops that are a bit more complex&mdash;when we have to step over some values in an interval.
23+
In the previous article in this [tutorial series](id:refactoring) we looked at converting simple loops written in the imperative style to the functional style. In this article we'll see how to take on loops that are a bit more complex&mdash;when we have to step over some values in an interval.
2424

2525
When looping over a range of values, one at a time, the `range()` method of `IntStream` came in handy to implement in the functional style. This method returns a stream that will generate one value at a time for values within the specified range. At first thought, to skip some values we may be tempted to use the `filter()` method on the stream. However, there's a simpler solution, the `iterate()` method of `IntStream`.
2626

app/pages/learn/01_tutorial/03_getting-to-know-the-language/03_refactoring_to_functional_style/03_converting_foreach_with_if.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ author: ["VenkatSubramaniam"]
2020
<a id="foreach">&nbsp;</a>
2121
## Iterating with foreach
2222

23-
In the previous articles in this series we looked at converting loops written in the imperative style to the functional style. In this article we'll see how to convert an imperative style iteration using `foreach` to the functional style. In addition, we'll also see how to pick select elements using `if` transforms to the functional style.
23+
In the previous articles in this [tutorial series](id:refactoring) we looked at converting loops written in the imperative style to the functional style. In this article we'll see how to convert an imperative style iteration using `foreach` to the functional style. In addition, we'll also see how to pick select elements using `if` transforms to the functional style.
2424

2525
Java 5 introduced the very popular `foreach` syntax. For example, to iterate over a collection of `String`s representing names, we'd write something like `for(String name: names)`. Under the hood, the `foreach` is converted, at the bytecode level, to use an `Iterator`&mdash;while the iterator tells us there is another element, fetch the next element for processing. In other words, the `foreach` is a nice concise syntax sugar for iteration with a `while` loop over the elements provided by an `Iterator`. We can convert a `foreach` into the functional style quite easily. Let's see how.
2626

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
---
2+
id: refactoring.iteration.withtransformation
3+
title: Converting Iteration with transformation
4+
slug: learn/refactoring-to-functional-style/iteartionwithtransformation
5+
type: tutorial-group
6+
group: refactoring-to-functional-style
7+
layout: learn/tutorial-group.html
8+
subheader_select: tutorials
9+
main_css_id: learn
10+
toc:
11+
- Transforming while Iterating {transforming}
12+
- From Imperative to Functional Style {imperativetofunctional}
13+
- Picking Elements to Transform {picking}
14+
- Mappings {mappings}
15+
description: "Converting Imperative Iteration with transformation to Functional Style."
16+
last_update: 2024-01-08
17+
author: ["VenkatSubramaniam"]
18+
---
19+
20+
<a id="transforming">&nbsp;</a>
21+
## Transforming while Iterating
22+
23+
In the previous articles in this [tutorial series](id:refactoring) we looked at converting loops with `if` or conditional statements in the imperative style to the functional style. In this article we'll see how to convert an imperative style iteration that transforms data to the functional style. In addition, we'll also refactor code that mixes transforming data with code that picks select elements before the transformation.
24+
25+
Anytime we are transforming data in an imperative style loop, we can use the `map()` function in the functional style. Let's see how.
26+
27+
28+
<a id="imperativetofunctional">&nbsp;</a>
29+
## From Imperative to Functional Style
30+
31+
Here's an example of an iteration, using the `foreach`, that transforms to uppercase a collection of names:
32+
33+
```java
34+
List<String> names = List.of("Jack", "Paula", "Kate", "Peter");
35+
36+
for(String name: names) {
37+
System.out.println(name.toUpperCase());
38+
}
39+
```
40+
41+
Each step through the iteration, the `name` variable is bound to a new value. As the iteration advances from one element to the next in the given collection, each name is transformed to uppercase using the `toUpperCase()` function and the resulting value is printed. We have already seen, in the previous article, how to convert the imperative style `foreach` to the functional style&mdash;using the `stream()` internal iteration. If we merely apply what we have seen before, the resulting functional style code would be rather unwieldy, with the lambda passed to `forEach` performing both transformation and printing, like so:
42+
43+
```java
44+
List<String> names = List.of("Jack", "Paula", "Kate", "Peter");
45+
46+
names.forEach(name -> System.out.println(name.toUpperCase())); //Don't do this
47+
```
48+
49+
Even though the above functional style code will produce the same results as the imperative style code, the lambda passed to the `forEach()` function is not cohesive, hard to read, and hard to change.
50+
51+
Before refactoring the imperative style code above to the function style, we should first refactor the imperative style to yet another imperative style implementation to make each line more cohesive, like so:
52+
53+
```java
54+
List<String> names = List.of("Jack", "Paula", "Kate", "Peter");
55+
56+
for(String name: names) {
57+
String nameInUpperCase = name.toUpperCase();
58+
System.out.println(nameInUpperCase);
59+
}
60+
```
61+
62+
From the previous articles in this series, we know that the `for` can turn into a `stream()` and the printing of the value can be done from within the `forEach()`. That leaves us with the transformation, the call to the `toUpperCase()` function. Such transformations can be done using the `map()` operation on the `stream()`.
63+
64+
```java
65+
List<String> names = List.of("Jack", "Paula", "Kate", "Peter");
66+
67+
names.stream()
68+
.map(name -> name.toUpperCase())
69+
.forEach(nameInUpperCase -> System.out.println(nameInUpperCase));
70+
```
71+
The `map()` operation transforms the data to a different value based on the function invoked from within the lambda expression that's passed to `map()`. In this example, each name is transformed to its uppercase value. The transformed value is then printed out from within the lambda expression passed to `forEach()`.
72+
73+
We can make the code a bit more concise by using method references instead of lambda expressions, like so:
74+
75+
```java
76+
List<String> names = List.of("Jack", "Paula", "Kate", "Peter");
77+
78+
names.stream()
79+
.map(String::toUpperCase)
80+
.forEach(System.out::println);
81+
```
82+
83+
Use the `map()` function to transform data while iterating over a collection of data.
84+
85+
<a id="picking">&nbsp;</a>
86+
## Picking Elements to Transform
87+
88+
Suppose, in the middle of the iteration, we want to pick some values from the collection based on some condition and apply a transformation only on those elements. For example, what if we want to transform and print only names of length 4? In the imperative style we could do the following:
89+
90+
```java
91+
List<String> names = List.of("Jack", "Paula", "Kate", "Peter");
92+
93+
for(String name: names) {
94+
if(name.length() == 4) {
95+
System.out.println(name.toUpperCase());
96+
}
97+
}
98+
```
99+
100+
We already know that the imperative style `if` can be refactored to the `filter()` function in the functional style. We can perform the transformation, using `map()`, after the `filter()` operation, like so:
101+
102+
```java
103+
List<String> names = List.of("Jack", "Paula", "Kate", "Peter");
104+
105+
names.stream()
106+
.filter(name -> name.length() == 4)
107+
.map(String::toUpperCase)
108+
.forEach(System.out::println);
109+
```
110+
111+
The `filter()` function discards data that's not desired and passes on only the values we like to use. The `map()` function transforms the values it sees after the filter.
112+
113+
<a id="mappings">&nbsp;</a>
114+
## Mappings
115+
116+
Anywhere you see transformation of data within a for loop, use the `map()` function to perform the transformation in the functional style. In addition, if the body of the loop has a `if` statement to selectively some value for transformation, then use the `stream()` API with the call to the `filter()` method before using the `map()` method.

0 commit comments

Comments
 (0)