
dtonhofer
Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 187 ff “Refactoring For Each”
Nothing special here, same changes as formerly, but we are using people.isEmpty()
instead of people.size() == 0
for even better legibility.
package chapter11;
import org.junit.jupiter.api.Test;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.*;
public class ForEachTest {
record Person(String name, int age) {
}
interface Agency {
boolean isChaperoneRequired(Set<Person> people);
}
static class AgencyBefore implements Agency {
public boolean isChaperoneRequired(Set<Person> people) {
boolean required = true;
if (people.isEmpty()) {
required = false;
} else {
for (var person : people) {
if (person.age() >= 18) {
required = false;
break;
}
}
}
return required;
}
}
static class AgencyAfter implements Agency {
public boolean isChaperoneRequired(Set<Person> people) {
return !people.isEmpty() &&
people.stream()
.noneMatch(person -> person.age() >= 18);
}
}
private static void commonAgencyTests(Agency agency) {
assertAll(
() -> assertTrue(agency.isChaperoneRequired(
Set.of(new Person("Jake", 12)))),
() -> assertTrue(agency.isChaperoneRequired(
Set.of(new Person("Jake", 12), new Person("Pam", 14)))),
() -> assertTrue(agency.isChaperoneRequired(
Set.of(new Person("Shiv", 8),
new Person("Sam", 9), new Person("Jill", 11)))),
() -> assertFalse(agency.isChaperoneRequired(
Set.of(new Person("Jake", 12), new Person("Pam", 18)))),
() -> assertFalse(agency.isChaperoneRequired(Set.of()))
);
}
@Test
void agencyBefore() {
commonAgencyTests(new AgencyBefore());
}
@Test
void agencyAfter() {
commonAgencyTests(new AgencyAfter());
}
}
Popular Pragmatic topics

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

Hello Brian,
I have some problems with running the code in your book. I like the style of the book very much and I have learnt a lot as...
New

Hi! I know not the intentions behind this narrative when called, on page XI:
mount() |> handle_event() |> render()
but the correc...
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

I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New

I think I might have found a problem involving SwitchCompat, thumbTint, and trackTint.
As entered, the SwitchCompat changes color to hol...
New

In general, the book isn’t yet updated for Phoenix version 1.6. On page 18 of the book, the authors indicate that an auto generated of ro...
New

Hey there,
I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New

I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1:
programming_ML/code/03_...
New

Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
Other popular topics

No chair. I have a standing desk.
This post was split into a dedicated thread from our thread about chairs :slight_smile:
New

Why, if your answer is yes?
New

There’s a whole world of custom keycaps out there that I didn’t know existed!
Check out all of our Keycaps threads here:
https://forum....
New

Just done a fresh install of macOS Big Sur and on installing Erlang I am getting:
asdf install erlang 23.1.2
Configure failed.
checking ...
New

Not sure if following fits exactly this thread, or if we should have a hobby thread…
For many years I’m designing and building model air...
New

“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco
Ever wonder how authoring books compares to writing articles?...
New

Build highly interactive applications without ever leaving Elixir, the way the experts do. Let LiveView take care of performance, scalabi...
New

Think Again 50% Off Sale »
The theme of this sale is new perspectives on familiar topics.
Enter coupon code ThinkAgain2021 at checkout t...
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

Author Spotlight:
Tammy Coron
@Paradox927
Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New
Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /scala
- /zig
- /html
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /textmate
- /sublime-text
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /slackware
- /julia
- /c
- /neovim