
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

As per the title, thanks.
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

Hi @Margaret ,
On page VII the book tells us the example and snippets will be all using Elixir version 1.11
But on page 3 almost the en...
New

I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
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

@parrt
In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
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

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
Other popular topics

I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
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

Do the test and post your score :nerd_face:
:keyboard:
If possible, please add info such as the keyboard you’re using, the layout (Qw...
New

In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first:
asdf plugin-upd...
New

Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
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

Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New

If you want a quick and easy way to block any website on your Mac using Little Snitch simply…
File > New Rule:
And select Deny, 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
- /typescript
- /svelte
- /crystal
- /kotlin
- /c-plus-plus
- /tailwind
- /gleam
- /ocaml
- /react
- /flutter
- /elm
- /vscode
- /ash
- /html
- /opensuse
- /centos
- /php
- /deepseek
- /zig
- /scala
- /textmate
- /lisp
- /sublime-text
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /spring
- /manjaro
- /nodejs
- /diversity
- /lua
- /julia
- /slackware
- /c
- /markdown