Skip to content

Commit 347b846

Browse files
authored
Merge branch 'main' into cert-jeanne
2 parents b4d7719 + 4ed0f16 commit 347b846

File tree

8 files changed

+529
-6
lines changed

8 files changed

+529
-6
lines changed

app/data/authors.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@
3939
website: https://agiledeveloper.com/
4040
description: |
4141
Venkat Placeholder.
42-
43-
42+
4443
- name: Jeanne Boyarsky
4544
email: nyjeanne@gmail.com
4645
photo_url: https://www.selikoff.net/wp-content/uploads/2019/05/jeanne-headshot.jpg

app/pages/community/jcs/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ subheader_select: jcs
77

88
<img src="/assets/images/jc-program-logo.png" alt="jc-logo" id="jc-logo" />
99

10-
The Java Champions Program consists of a unique group of skilled Java technologists and community leaders sponsored by Oracle. Java Champions come from a broad cross-section of the Java community. They are leaders, influencers and enablers who help grow the size of the Java community. Java Champions are active in many ways such as actively participating in Java projects, engaging with Java User Group communities, speaking at conferences, authoring content, teaching other developers, fostering inclusive participation and so much more.
10+
The Java Champions Program consists of a unique group of skilled Java technologists and community leaders. Java Champions come from a broad cross-section of the Java community. They are leaders, influencers and enablers who help grow the size of the Java community. Java Champions are active in many ways such as actively participating in Java projects, engaging with Java User Group communities, speaking at conferences, authoring content, teaching other developers, fostering inclusive participation and so much more.
1111

12-
They are technical and community luminaries; some are Java engineers or architects who are relatively skilled and has lots of experience, or are community builders who take time to build personal, long-lasting relationships that go beyond programming. Java Champions are independent-minded and credible, who embody the best virtues of the Java community including honesty, dedication and sincerity.
12+
They are technical and community luminaries; some are Java engineers or architects who are relatively skilled and have lots of experience, or are community builders who take time to build personal, long-lasting relationships that go beyond programming. Java Champions are independent-minded and credible, who embody the best virtues of the Java community including honesty, dedication and sincerity.
1313

1414
Java Champions are also able to advocate or influence other developers through their own professional activities (via consulting, teaching, writing, speaking, etc.). They have the opportunity to provide independent, constructive feedback and ideas absent of a competitive agenda that will help Oracle continue to move Java forward. This interchange may be in the form of technical discussions and/or community-building activities with Oracle's Java Advocacy Team.
1515

app/pages/learn/01_tutorial/02_new-features/00_virtual-threads.md

Lines changed: 376 additions & 0 deletions
Large diffs are not rendered by default.

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
@@ -20,4 +20,5 @@ In this series we cover the following conversions from the imperative to the fun
2020
| Tutorial |Imperative Style | Functional Style Equivalent |
2121
|------------------------------------------------------|-----------------|------------------------------|
2222
| [Converting Simple Loops](id:refactoring.simple.loops) | `for()` | `range()` or `rangeClosed()` |
23+
| [Converting Loops with Steps](id:refactoring.loops.withsteps) | `for(...i = i + ...)` | `iterate()` with `takeWhile()` |
2324

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
---
2+
id: refactoring.loops.withsteps
3+
title: Converting Loops with Steps
4+
slug: learn/refactoring-to-functional-style/loopswithsteps
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+
- Iterating with Steps {steps}
12+
- From Imperative to Functional Style {imperativetofunctional}
13+
- Unbounded Iteration with a break {break}
14+
- Mappings {mappings}
15+
description: "Converting Imperative Loops with Steps to Functional Style."
16+
last_update: 2023-07-06
17+
author: ["VenkatSubramaniam"]
18+
---
19+
20+
<a id="steps">&nbsp;</a>
21+
## Iterating with Steps
22+
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.
24+
25+
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`.
26+
27+
<a id="imperativetofunctional">&nbsp;</a>
28+
## From Imperative to Functional Style
29+
30+
Here's a loop that uses step to skip a few values in the desired range:
31+
32+
```java
33+
for(int i = 0; i < 15; i = i + 3) {
34+
System.out.println(i);
35+
}
36+
```
37+
38+
The value of the index variable `i` starts at `0` and then is incremented by `3` as the iteration moves forward. When you find yourself looking at a loop like that where the iteration is not over every single value in a range, but some values are skipped, consider using the `iterate()` method of `IntStream`.
39+
40+
Before we refactor the code, let's take a closer look at the `for()` loop in the previous code, but with a pair of imaginary glasses that let us look at potential uses for lambdas.
41+
42+
```java
43+
//imaginary code
44+
for(int i = 0; i < 15; i = i + 3) //imperative
45+
for(seed, i -> i < 15, i -> i + 3) //functional
46+
```
47+
48+
The first argument passed to the `for` loop is the starting value or the seed for the iteration and it can stay as is. The second argument is a predicate that tells the value of the index variable, `i`, should not exceed the value of `15`. We can replace that in the functional style with a `IntPredicate`. The third argument is incrementing the value of the index variable and that, in functional style, is simply a `IntUnaryOperator`. The `IntStream` interface has a `static` method named `iterate()` that nicely represents the imaginary code: `iterate(int seed, IntPredicate hasNext, IntUnaryOperator next)`.
49+
50+
Let's refactor the loop to use functional style.
51+
52+
```java
53+
import java.util.stream.IntStream;
54+
55+
...
56+
IntStream.iterate(0, i -> i < 15, i -> i + 3)
57+
.forEach(System.out::println);
58+
```
59+
60+
That was pretty straightforward, the `;`s became `,`s, we made use of two lambdas: one for the `IntPredicate` and the other for the `IntUnaryOperator`.
61+
62+
In addition to stepping over values, we often use an unbounded loop and that throws a bit more complexity on us, but nothing the functional APIs of Java can't handle, as we'll see next.
63+
64+
<a id="break">&nbsp;</a>
65+
## Unbounded Iteration with a break
66+
67+
Let's take a look at the following imperative style loop which, in addition to the step, is unbounded and uses the `break` statement.
68+
69+
```java
70+
for(int i = 0;; i = i + 3) {
71+
if(i > 20) {
72+
break;
73+
}
74+
75+
System.out.println(i);
76+
}
77+
```
78+
79+
The terminating condition of `i < 15` is gone and the loop is unbounded as indicated by the repeated `;;`s. Within the loop, however, we have the `break` statement to exit out of the iteration if the value of `i` is greater than `20`.
80+
81+
For the functional style, we can get rid of the second argument, the `IntPredicate` from the `iterate()` method call but that will turn the iteration into an infinite stream. The functional programming equivalent of the imperative style `break` is the `takeWhile()` method. This method will terminate the internal iterator, the stream, if the `IntPredicate` passed to it evaluates to `false`. Let's refactor the previous imperative style unbounded `for` with `break` to functional style.
82+
83+
```java
84+
IntStream.iterate(0, i -> i + 3)
85+
.takeWhile(i -> i <= 20)
86+
.forEach(System.out::println);
87+
```
88+
89+
The `iterate()` method is overloaded and comes in two flavors, one with the `IntPredicate` and the other without. We made use of the version without the predicate to create an infinite stream that generates values from the seed or the starting value. The `IntUnaryOperator` passed as the second argument determines the steps. Thus, in the given code example, the stream will generate values `0`, `3`, `6`, and so on. Since we want to limit the iteration so that the index does not exceed the value of `20` we use the `takeWhile()`. The predicate passed in to `takeWhile()` tells that the iteration may continue as long as the value of the parameter given, the index `i`, does not exceed the value of `20`.
90+
91+
We saw in the previous article that `range()` and `rangeClosed()` are direct replacement for the simple `for` loop. If the loop gets a bit more complex, no worries, Java has you covered, you can use the `IntStream`'s `iterate()` method and optionally the `takeWhile()` if the loop is terminated using `break`.
92+
93+
<a id="mappings">&nbsp;</a>
94+
## Mappings
95+
96+
Anywhere you see a `for` loop with step, use the `iterate()` method with three arguments, a seed or the starting value, a `IntPredicate` for the terminating condition, and a `IntUnaryOperator` for the steps. If your loop uses the `break` statement, then drop the `IntPredicate` from the `iterate()` method call and instead use the `takeWhile()` method. The `takeWhile()` is the functional equivalent of the imperative style `break`.
97+

app/templates/pages/learn/index.html

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,18 @@ <h2>Running Your First Java Application</h2>
2828
</ul>
2929
</div>
3030

31+
<div class="learn-group">
32+
<h2>Staying Aware of New Features</h2>
33+
<ul>
34+
{% for tutorial in file.data.tutorials.awareness | sort(false, false, "category_order") %}
35+
<li>
36+
<a href="/{{ tutorial.path }}">{{ tutorial.title_html | safe }}</a><br />
37+
<div class="desc">{{ tutorial.description_html | safe }}</div>
38+
</li>
39+
{% endfor %}
40+
</ul>
41+
</div>
42+
3143

3244
<div class="learn-group">
3345
<h2>Getting to Know the Language</h2>
@@ -41,6 +53,42 @@ <h2>Getting to Know the Language</h2>
4153
</ul>
4254
</div>
4355

56+
<div class="learn-group">
57+
<h2>Mastering the API</h2>
58+
<ul>
59+
{% for tutorial in file.data.tutorials.api | sort(false, false, "category_order") %}
60+
<li>
61+
<a href="/{{ tutorial.path }}">{{ tutorial.title_html | safe }}</a><br />
62+
<div class="desc">{{ tutorial.description_html | safe }}</div>
63+
</li>
64+
{% endfor %}
65+
</ul>
66+
</div>
67+
68+
<div class="learn-group">
69+
<h2>Organizing your Application</h2>
70+
<ul>
71+
{% for tutorial in file.data.tutorials.organizing | sort(false, false, "category_order") %}
72+
<li>
73+
<a href="/{{ tutorial.path }}">{{ tutorial.title_html | safe }}</a><br />
74+
<div class="desc">{{ tutorial.description_html | safe }}</div>
75+
</li>
76+
{% endfor %}
77+
</ul>
78+
</div>
79+
80+
<div class="learn-group">
81+
<h2>Getting to know the JVM</h2>
82+
<ul>
83+
{% for tutorial in file.data.tutorials.jvm | sort(false, false, "category_order") %}
84+
<li>
85+
<a href="/{{ tutorial.path }}">{{ tutorial.title_html | safe }}</a><br />
86+
<div class="desc">{{ tutorial.description_html | safe }}</div>
87+
</li>
88+
{% endfor %}
89+
</ul>
90+
</div>
91+
4492
{{ contents | safe }}
4593
</div>
4694

package-lock.json

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
},
3030
"devDependencies": {
3131
"dotenv": "^16.0.3",
32-
"gulp-markdown-toc": "^1.1.0"
32+
"gulp-markdown-toc": "^1.1.0",
33+
"gulp-util": "^3.0.8"
3334
}
3435
}

0 commit comments

Comments
 (0)