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

Hi Brian,
Looks like the api for tinydb has changed a little. Noticed while working on chapter 7 that the .purge() call to the db throws...
New

your book suggests to use Image.toByteData() to convert image to bytes, however I get the following error: "the getter ‘toByteData’ isn’t...
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

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

Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below:
d...
New

Modern front-end development for Rails, second edition - Struggling to get the first chapter to work
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
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

@mfazio23
I’ve applied the changes from Chapter 5 of the book and everything builds correctly and runs. But, when I try to start a game,...
New

@mfazio23
Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
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

Reading something? Working on something? Planning something? Changing jobs even!?
If you’re up for sharing, please let us know what you’...
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:
https://on...
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

I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me.
I think the Planck and the Preonic (o...
New

If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New

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

Seems like a lot of people caught it - just wondered whether any of you did?
As far as I know I didn’t, but it wouldn’t surprise me if I...
New

Author Spotlight:
Sophie DeBenedetto
@SophieDeBenedetto
The days of the traditional request-response web application are long gone, b...
New

A Brief Review of the Minisforum V3 AMD Tablet.
Update: I have created an awesome-minisforum-v3 GitHub repository to list information fo...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /java
- /haskell
- /onivim
- /svelte
- /typescript
- /crystal
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /react
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /scala
- /zig
- /textmate
- /sublime-text
- /debian
- /nixos
- /lisp
- /agda
- /react-native
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /spring
- /django
- /manjaro
- /diversity
- /nodejs
- /lua
- /julia
- /c
- /slackware
- /markdown