
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 Prag Prog topics

Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84:
bundle exec sequel...
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

I ran this command after installing the sample application:
$ cards add do something --owner Brian
And got a file not found error:
Fil...
New

@noelrappin
Running the webpack dev server, I receive the following warning:
ERROR in tsconfig.json
TS18003: No inputs were found in c...
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

When running tox for the first time, I got the following error:
ERROR: InterpreterNotFound: python3.10
I realised that I was running ...
New

Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New

Hi, I’m working on the Chapter 8 of the book.
After I add add the point_offset, I’m still able to see acne:
In the image above, I re...
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

From page 13:
On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New
Other popular topics

@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness.
https://f...
New

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

A PragProg Hero’s Journey with Brian P. Hogan @bphogan
Have you ever worried that your only legacy will be in the form of legacy...
New

New

Why, if your answer is yes?
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
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

I have always used antique keyboards like Cherry MX 1800 or Cherry MX 8100 and almost always have modified the switches in some way, like...
New

This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
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
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
- /textmate
- /sublime-text
- /react-native
- /kubuntu
- /arch-linux
- /revery
- /ubuntu
- /manjaro
- /django
- /spring
- /diversity
- /nodejs
- /lua
- /c
- /slackware
- /julia
- /neovim