
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

page 20: … protoc command…
I had to additionally run the following go get commands in order to be able to compile protobuf code using 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

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

The generated iex result below should list products instead of product for the metadata. (page 67)
iex> product = %Product{}
%Pento....
New

Hello! Thanks for the great book.
I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New

The allprojects block listed on page 245 produces the following error when syncing gradle:
“org.gradle.api.GradleScriptException: A prob...
New

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

I just bought this book to learn about Android development, and I’m already running into a major issue in Ch. 1, p. 20: “Update activity...
New

From page 13:
On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New
Other popular topics

SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
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

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

The V Programming Language
Simple language for building maintainable programs
V is already mentioned couple of times in the forum, but I...
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

Hi folks,
I don’t know if I saw this here but, here’s a new programming language, called Roc
Reminds me a bit of Elm and thus Haskell. ...
New

Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New

Author Spotlight:
David Bryant Copeland
@davetron5000
We’re so happy to bring you another Author Spotlight, a series where we sit dow...
New

I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /scala
- /zig
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /react-native
- /sublime-text
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /c
- /slackware
- /julia
- /neovim