dtonhofer
Functional Programming in Java, Second Edition: Functional Programming in Java, Second Edition: JUnit code improvements for Chapter 11, pages 193 ff “Refactoring Data Grouping Operations”
As usual, but the original code to be refactored was just too evil and had to be fixed.
package chapter11;
import org.junit.jupiter.api.Test;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.toSet;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DataGroupingOperationsTest {
interface Scores {
Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores);
}
public static class ScoresBefore implements Scores {
public Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores) {
final Map<Integer, Set<String>> result = new HashMap<>();
for (Map.Entry<String, Integer> entry : scores.entrySet()) {
final String name = entry.getKey();
final Integer score = entry.getValue();
if (!result.containsKey(score)) {
result.put(score, new HashSet<>());
}
result.get(score).add(name);
}
return result;
}
}
public static class ScoresAfter implements Scores {
public Map<Integer, Set<String>> namesForScores(final Map<String, Integer> scores) {
return scores.keySet().stream()
.collect(groupingBy(scores::get, toSet()));
}
}
private static void commonNamesForScoresTests(final Scores scores) {
assertAll(
() -> assertEquals(Map.of(), scores.namesForScores(Map.of())),
() -> assertEquals(
Map.of(1, Set.of("Jill")), scores.namesForScores(Map.of("Jill", 1))),
() -> assertEquals(
Map.of(1, Set.of("Jill"), 2, Set.of("Paul")),
scores.namesForScores(Map.of("Jill", 1, "Paul", 2))),
() -> assertEquals(
Map.of(1, Set.of("Jill", "Kate"), 2, Set.of("Paul")),
scores.namesForScores(Map.of("Jill", 1, "Paul", 2, "Kate", 1)))
);
}
@Test
void scoresBefore() {
commonNamesForScoresTests(new ScoresBefore());
}
@Test
void scoresAfter() {
commonNamesForScoresTests(new ScoresAfter());
}
}
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
Working through the steps (checking that the Info,plist matches exactly), run the demo game and what appears is grey but does not fill th...
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
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
Hi,
build fails on:
bracket-lib = “~0.8.1”
when running on Mac Mini M1 Rust version 1.5.0:
Compiling winit v0.22.2
error[E0308]: mi...
New
First, the code resources:
Page 237: rumbl_umbrella/apps/rumbl/mix.exs
Note: That this file is missing.
Page 238: rumbl_umbrella/app...
New
A Common-Sense Guide to Data Structures and Algorithms, Second Edition by Jay Wengrow @jaywengrow
Hi,
I have the paperback version of t...
New
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
Hi,
I completed chapter 6 but am getting the following error when running:
thread 'main' panicked at 'Failed to load texture: IoError(O...
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
Other popular topics
Brace yourself for a fun challenge: build a photorealistic 3D renderer from scratch! In just a couple of weeks, build a ray tracer that r...
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
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me.
I think the Planck and the Preonic (o...
New
Think Again 50% Off Sale »
The theme of this sale is new perspectives on familiar topics.
Enter coupon code ThinkAgain2021 at checkout t...
New
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
Was just curious to see if any were around, found this one:
I got 51/100:
Not sure if it was meant to buy I am sure at times the b...
New
New
Develop, deploy, and debug BEAM applications using BEAMOps: a new paradigm that focuses on scalability, fault tolerance, and owning each ...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /zig
- /deepseek
- /scala
- /sublime-text
- /textmate
- /lisp
- /react-native
- /debian
- /nixos
- /agda
- /kubuntu
- /arch-linux
- /deno
- /django
- /ubuntu
- /revery
- /spring
- /manjaro
- /nodejs
- /diversity
- /lua
- /julia
- /c
- /slackware







