dtonhofer

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());
    }
}
0 369 0

Where Next?

Popular Pragmatic Bookshelf topics Top

abtin
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...
14 2929 7
New
jimmykiang
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:...
4 1547 7
New
jimschubert
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
8 1310 3
New
jon
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...
10 2622 9
New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
5 3547 1
New
jdufour
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… ...
3 1140 1
New
raul
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? ...
0 1297 3
New
alanq
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...
1 1367 7
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
1 1111 3
New
ggerico
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_...
1 3040 5
New

Other popular topics Top

AstonJ
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-...
10 5348 11
New
AstonJ
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...
88 5364 32
New
Exadra37
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 ...
66 21694 24
New
Exadra37
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...
10 5256 6
New
Margaret
Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
1143 25883 760
New
foxtrottwist
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...
52 4894 22
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
15 5398 1
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
106 10605 28
New
hilfordjames
There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
3 1664 2
New
AnfaengerAlex
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...
0 2183 2
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: