Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 195 ff “Refactoring Nested Loops"

Nothing surprising here, just adapting the form to the other examples.
Ah yes, compute()
now returns an immutable list in all cases.
package chapter11;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class NestedLoopTest {
record Triple(int a, int b, int c) {
public String toString() {
return String.format("%d %d %d", a, b, c);
}
}
private static Triple triple(int a, int b, int c) {
return new Triple(a, b, c);
}
interface PythagoreanTriples {
List<Triple> compute(int numberOfValues);
}
// (m² - n², 2 * m * n, m² + n²) is a valid triple because, resolving
// (m⁴ + n⁴ - 2 * m² * n²) + (4 * m² * n²) = m⁴ + n⁴ + 2 * m² * n²
// assuming m > n
private static Triple getTripleEuclidsWay(final int m, final int n) {
if (m <= n) {
throw new IllegalArgumentException("m <= n");
}
final int a = m * m - n * n;
final int b = 2 * m * n;
final int c = m * m + n * n;
return triple(a, b, c);
}
static class PythagoreanTriplesBefore implements PythagoreanTriples {
public List<Triple> compute(int tripleCount) {
if (tripleCount == 0) {
return List.of(); // unmodifiable
}
List<Triple> triples = new ArrayList<>();
for (int m = 2; ; m++) {
for (int n = 1; n < m; n++) {
triples.add(getTripleEuclidsWay(m, n));
if (triples.size() == tripleCount)
break;
}
if (triples.size() == tripleCount)
break;
}
return Collections.unmodifiableList(triples);
}
}
static class PythagoreanTriplesAfter implements PythagoreanTriples {
public List<Triple> compute(int tripleCount) {
return Stream.iterate(2, e -> e + 1)
.flatMap(m -> IntStream.range(1, m).mapToObj(n -> getTripleEuclidsWay(m, n)))
.limit(tripleCount)
.toList();
}
}
private static void commonPythagoreanTriplesTests(final PythagoreanTriples pytris) {
assertAll(
() -> assertEquals(List.of(), pytris.compute(0)),
() -> assertEquals(List.of(triple(3, 4, 5)),
pytris.compute(1)),
() -> assertEquals(
List.of(triple(3, 4, 5), triple(8, 6, 10), triple(5, 12, 13)),
pytris.compute(3)),
() -> assertEquals(
List.of(triple(3, 4, 5), triple(8, 6, 10),
triple(5, 12, 13), triple(15, 8, 17),
triple(12, 16, 20)),
pytris.compute(5))
);
}
@Test
void pythagoreanTriplesBefore() {
commonPythagoreanTriplesTests(new PythagoreanTriplesBefore());
}
@Test
void pythagoreanTriplesAfter() {
commonPythagoreanTriplesTests(new PythagoreanTriplesAfter());
}
}
Popular Pragprog topics

The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New

Title: Web Development with Clojure, Third Edition, pg 116
Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New

Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New

Title: Web Development with Clojure, Third Edition, vB17.0 (p9)
The create table guestbook syntax suggested doesn’t seem to be accepted ...
New

Dear Sophie.
I tried to do the “Authorization” exercise and have two questions:
When trying to plug in an email-service, I found the ...
New

Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64)
If you follow the defau...
New

When installing Cards as an editable package, I get the following error:
ERROR: File “setup.py” not found. Directory cannot be installe...
New

When running tox for the first time, I got the following error:
ERROR: InterpreterNotFound: python3.10
I realised that I was running ...
New

Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New

Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
Other popular topics

Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it.
Does your monitor have eye p...
New

I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New

My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New

Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face:
...
New
New

“Finding the Boundaries” Hero’s Journey with Noel Rappin @noelrappin
Even when you’re ultimately right about what the future ho...
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New

API 4
Path:
/user/following/
Method:
GET
Description:
Returns the list of all names of people whom the user follows
Response
[
{ ...
New

Author Spotlight: Dmitry Zinoviev (@aqsaqal)
Today we’re putting our spotlight on Dmitry Zinoviev, author of Data Science Essentials ...
New

This is a very quick guide, you just need to:
Download LM Studio: https://lmstudio.ai/
Click on search
Type DeepSeek, then select the o...
New
Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /flutter
- /preact
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm