 
  		        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
 
          
                When I try the command to create a pair of migration files I get an error. 
user=> (create-migration "guestbook")
Execution error (Ill...
              
            
            
          
              New
 
          
                I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps. 
With: 
ruby-3.0.0 
s...
              
            
            
          
              New
 
          
                #book-python-testing-with-pytest-second-edition 
Hi. Thanks for writing the book. I am just learning so this might just of been an issue ...
              
            
            
          
              New
 
          
                I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
              
            
            
          
              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
 
          
                Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on  (page 64) 
If you follow the defau...
              
            
            
          
              New
 
          
                On page 78 the following code appears: 
<%= link_to ‘Destroy’, product, 
class: ‘hover:underline’, 
method: :delete, 
data: { confirm...
              
            
            
          
              New
 
          
                It seems the second code snippet is missing the code to set the current_user: 
current_user: Accounts.get_user_by_session_token(session["...
              
            
            
          
              New
 
          
                Hello faithful readers!  If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
              
            
            
          
              New
Other popular topics
                         
                      
                       
          
                Algorithms and data structures are much more than abstract concepts. Mastering them enables you to write code that runs faster and more e...
              
            
            
              
          
              New
 
          
                I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? 
Also, I want to know if someone is using it for program...
              
            
            
          
              New
 
          
                Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face: 
Perhaps if there’s enough peop...
              
            
            
          
              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
 
          
                Biggest jackpot ever apparently! :upside_down_face: 
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
              
            
            
          
              New
 
          
                A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
              
            
            
          
              New
 
          
                This is going to be a long an frequently posted thread. 
While talking to a friend of mine who has taken data structure and algorithm cou...
              
            
            
          
              New
 
          
                Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
              
            
            
              
          
              New
 
          
                There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
              
            
            
          
              New
 
          
                Big O Notation can make your code faster by orders of magnitude. Get the hands-on info you need to master data structures and algorithms ...
              
            
            
              
          
              New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /rails
- /js
- /security
- /go
- /swift
- /vim
- /clojure
- /haskell
- /emacs
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /php
- /zig
- /deepseek
- /scala
- /lisp
- /textmate
- /sublime-text
- /react-native
- /nixos
- /debian
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /deno
- /ubuntu
- /spring
- /nodejs
- /manjaro
- /diversity
- /lua
- /julia
- /slackware
- /c
 
    





