
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

This test is broken right out of the box…
— FAIL: TestAgent (7.82s)
agent_test.go:77:
Error Trace: agent_test.go:77
agent_test.go:...
New

In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New

Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online:
p. 186 But...
New

Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’.
In particula...
New

Hello! On page xix of the preface, it says there is a community forum "… for help if your’re stuck on one of the exercises in this book… ...
New

Page 28: It implements io.ReaderAt on the store type.
Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt?
...
New

This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
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 fore...
New

I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1:
programming_ML/code/03_...
New
Other popular topics

poll
poll
Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New

Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face:
https://on...
New

I am asking for any distro that only has the bare-bones to be able to get a shell in the server and then just install the packages as we ...
New

Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New

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

Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New

Author Spotlight
Rebecca Skinner
@RebeccaSkinner
Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New

There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
New

Hello,
I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /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
- /php
- /deepseek
- /html
- /zig
- /scala
- /textmate
- /sublime-text
- /nixos
- /debian
- /lisp
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /django
- /spring
- /manjaro
- /nodejs
- /diversity
- /lua
- /c
- /slackware
- /julia
- /neovim