
dtonhofer
Functional Programming in Java, Second Edition: Nicer code pf page 7
I suggest this nicer code for the Statistics.java on page on page 70 (we are doing streams after).
Not using a main()
but a JUnit 5 Test Case:
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.averagingDouble;
import static java.util.stream.Collectors.summarizingDouble;
public class TransformingData {
record Pair(String str, Number num) {
public String toString() {
return str + ": " + num;
}
}
@Test
public void statistics() {
var statistics = Person.getPeople().stream()
.collect(
summarizingDouble(person -> person.emailAddresses().size()));
var pairs = new ArrayList<Pair>();
pairs.add(new Pair("Number of people", statistics.getCount()));
pairs.add(new Pair("Number of email addresses", statistics.getSum()));
pairs.add(new Pair("Average number of email addresses", statistics.getAverage()));
pairs.add(new Pair("Max number of email addresses", statistics.getMax()));
pairs.add(new Pair("Min number of email addresses", statistics.getMin()));
String res = pairs.stream().map(Pair::toString).collect(Collectors.joining("\n"));
System.out.println(res);
}
}
Similary for the next exercises
record PairB(String str, Boolean b) {
public String toString() {
return str + ": " + b;
}
}
public void printPairsB(List<PairB> pairs) {
var res = pairs.stream().map(PairB::toString).collect(Collectors.joining("\n"));
System.out.println(res);
}
@Test
public void checkingForCriteriaAny() {
List<Person> people = Person.getPeople();
var pairs = new ArrayList<PairB>();
pairs.add(new PairB(
"Anyone has email address: ",
people.stream().anyMatch(person -> person.emailAddresses().size() > 0)));
pairs.add(new PairB(
"Anyone has more than 10 email address: ",
people.stream().anyMatch(person -> person.emailAddresses().size() > 10)));
printPairsB(pairs);
}
@Test
public void checkingForCriteriaAll() {
List<Person> people = Person.getPeople();
var pairs = new ArrayList<PairB>();
pairs.add(new PairB(
"Everyone has at least one email address: ",
people.stream().allMatch(person -> person.emailAddresses().size() > 0)));
pairs.add(new PairB(
"Everyone has zero or more email address: ",
people.stream().allMatch(person -> person.emailAddresses().size() >= 0)));
printPairsB(pairs);
}
And even the next
record PairN(String str, Number num) {
public String toString() {
return str + ": " + num;
}
}
public void printPairsN(List<PairN> pairs) {
var res = pairs.stream().map(PairN::toString).collect(Collectors.joining("\n"));
System.out.println(res);
}
@Test
public void partitioning() {
List<Person> people = Person.getPeople();
Map<Boolean, List<Person>> partitions =
people.stream()
.collect(partitioningBy(person -> person.emailAddresses().size() > 1));
var pairs = new ArrayList<PairN>();
pairs.add(new PairN("Number of people with at most one email address", partitions.get(false).size()));
pairs.add(new PairN("Number of people with multiple email addresses" , partitions.get(true).size()));
printPairsN(pairs);
}
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

page 37
ANTLRInputStream input = new ANTLRInputStream(is);
as of ANTLR 4 .8 should be:
CharStream stream = CharStreams.fromStream(i...
New

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

Title: Web Development with Clojure, Third Edition, pg 116
Hi - I just started chapter 5 and I am stuck on page 116 while trying to star...
New

Dear Sophie.
I tried to do the “Authorization” exercise and have two questions:
When trying to plug in an email-service, I found the ...
New

When trying to generate the protobuf .go file, I receive this error:
Unknown flag: --go_opt
libprotoc 3.12.3
MacOS 11.3.1
Googling ...
New

When trying to run tox in parallel as explained on page 151, I got the following error:
tox: error: argument -p/–parallel: expected one...
New

When running tox for the first time, I got the following error:
ERROR: InterpreterNotFound: python3.10
I realised that I was running ...
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

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

A thread that every forum needs!
Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New

Which, if any, games do you play? On what platform?
I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New

We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New

Small essay with thoughts on macOS vs. Linux:
I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New

Tailwind CSS is an exciting new CSS framework that allows you to design your site by composing simple utility classes to create complex e...
New

The File System Access API with Origin Private File System.
WebKit supports new API that makes it possible for web apps to create, open,...
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

This is a very quick guide, you just need to:
Download LM Studio: https://lmstudio.ai/
Click on search
Type DeepSeek, then select the o...
New

Background
Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /onivim
- /svelte
- /typescript
- /crystal
- /kotlin
- /c-plus-plus
- /tailwind
- /gleam
- /ocaml
- /react
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /centos
- /html
- /php
- /deepseek
- /zig
- /scala
- /lisp
- /textmate
- /sublime-text
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /revery
- /django
- /ubuntu
- /spring
- /manjaro
- /diversity
- /nodejs
- /lua
- /c
- /slackware
- /julia
- /markdown