
dtonhofer
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 Pragmatic Bookshelf topics

This test is broken right out of the box…
— FAIL: TestAgent (7.82s)
agent_test.go:77:
Error Trace: agent_test.go:77
agent_test.go:...
New

In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New

Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’.
In particula...
New

Hi @venkats,
It has been mentioned in the description of ‘Supervisory Job’ title that 2 things as mentioned below result in the same eff...
New

This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New

“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ”
Excerpt From: Br...
New

I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New

Hi,
I completed chapter 6 but am getting the following error when running:
thread 'main' panicked at 'Failed to load texture: IoError(O...
New

Hi all,
currently I wonder how the Tailwind colours work (or don’t work).
For example, in app/views/layouts/application.html.erb I have...
New

root_layout: {PentoWeb.LayoutView, :root},
This results in the following following error:
no “root” html template defined for PentoWeb...
New
Other popular topics

I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl:
The Ser...
New

This looks like a stunning keycap set :orange_heart:
A LEGENDARY KEYBOARD LIVES ON
When you bought an Apple Macintosh computer in the e...
New

Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
New

Create efficient, elegant software tests in pytest, Python's most powerful testing framework.
Brian Okken @brianokken
Edited by Kat...
New

Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New

Author Spotlight:
VM Brasseur
@vmbrasseur
We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New

Author Spotlight:
Peter Ullrich
@PJUllrich
Data is at the core of every business, but it is useless if nobody can access and analyze ...
New

Get the comprehensive, insider information you need for Rails 8 with the new edition of this award-winning classic.
Sam Ruby @rubys
...
New

This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
New

Ok, well here are some thoughts and opinions on some of the ergonomic keyboards I have, I guess like mini review of each that I use enoug...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /python
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /onivim
- /typescript
- /svelte
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /deepseek
- /zig
- /scala
- /textmate
- /lisp
- /sublime-text
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /manjaro
- /spring
- /nodejs
- /diversity
- /lua
- /deno
- /julia
- /slackware
- /c