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

Dockerfile:
FROM ruby:2.6
RUN apt-get update -yqq
RUN apt-get install -yqq --no-install-recommends nodejs
COPY . /usr/src/app/
WORKDI...
New

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

The answer to 3rd Problem of Chapter 5 (Making Choices) of “Practical Programming, Third Edition” seems incorrect in the given answer ke...
New

Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display)
It looks like
.with_simple_console_no_bg(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New

The generated iex result below should list products instead of product for the metadata. (page 67)
iex> product = %Product{}
%Pento....
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

Hi all,
currently I wonder how the Tailwind colours work (or don’t work).
For example, in app/views/layouts/application.html.erb I have...
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

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

Any thoughts on Svelte?
Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
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

SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New

Rust is an exciting new programming language combining the power of C with memory safety, fearless concurrency, and productivity boosters...
New

I have seen the keycaps I want - they are due for a group-buy this week but won’t be delivered until October next year!!! :rofl:
The Ser...
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

Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalabilit...
New

Author Spotlight: Mike Riley (@mriley)
This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s bo...
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...
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
Latest in Pragprog
Latest (all)
Categories:
My Saved Portals
-
None saved yet
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /clojure
- /react
- /quarkus
- /go
- /vapor
- /v
- /react-native
- /wasm
- /security
- /django
- /nodejs
- /centos
- /haskell
- /rails
- /fable
- /gleam
- /swift
- /js
- /deno
- /assemblyscript
- /tailwind
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /preact
- /flutter
- /c-plus-plus
- /actix
- /java
- /angular
- /ocaml
- /zig
- /kubuntu
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /html
- /keyboards
- /vuejs
- /nim
- /emacs
- /nerves
- /elm