dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: Code for "Chapter 7, Lazy Evaluations" in one file

Nothing special:

package chapter7;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Stream;

public class BeingLazy_LazyEvaluations {

    private final static List<String> names = List.of("Brad", "Kate", "Kim", "Jack", "Joe", "Mike", "Susan", "George", "Robert", "Julia", "Parker", "Benson");
    
    // "lazy/fpij/Evaluation.java" on p. 127, p.128

    private static boolean evaluate(final int value) {
        System.out.println("evaluating ..." + value);
        simulateTimeConsumingOp(2000);
        return value > 100;
    }

    private static void simulateTimeConsumingOp(long time_ms) {
        try {
            Thread.sleep(time_ms);
        } catch (InterruptedException ex) {
            throw new RuntimeException(ex);
        }
    }

    // "lazy/fpij/Evaluation.java" p. 127

    private static void eagerEvaluator(final boolean input1, final boolean input2) {
        System.out.println("eagerEvaluator called...");
        System.out.println("accept?: " + (input1 && input2));
    }

    // "lazy/fpij/Evaluation.java" p. 128

    private static void lazyEvaluator(final Supplier<Boolean> input1, final Supplier<Boolean> input2) {
        System.out.println("lazyEvaluator called...");
        System.out.println("accept?: " + (input1.get() && input2.get()));
    }

    // "lazy/fpij/LazyStreams.java" on p.130

    private static int length(final String name) {
        System.out.println("getting length for " + name);
        return name.length();
    }

    // "lazy/fpij/LazyStreams.java" on p.130

    private static String toUpper(final String name) {
        System.out.println("converting to uppercase: " + name);
        return name.toUpperCase();
    }

    // "lazy/fpij/Evaluation.java" on p.127

    @Test
    void evaluateEagerly() {
        eagerEvaluator(evaluate(1), evaluate(2));
    }

    // "lazy/fpij/Evaluation.java" on p.128

    @Test
    void evaluateLazily() {
        lazyEvaluator(() -> evaluate(1), () -> evaluate(2));
    }

    // "lazy/fpij/LazyStreams.java" on p.130
    // ".map(name -> toUpper(name))" replaced with ".map(LazyStreams::toUpper)"

    @Test
    void lazyStream() {
        final String firstNameWith3Letters =
                names.stream()
                        .filter(name -> length(name) == 3)
                        .map(BeingLazy_LazyEvaluations::toUpper)
                        .findFirst()
                        .orElse("");
        System.out.println(firstNameWith3Letters);
    }

    // lazy/fpij/LazyStreams.java on p.132
    // ".map(name -> toUpper(name))" replaced with ".map(LazyStreams::toUpper)"

    @Test
    void peekingIntoLaziness() {
        Stream<String> namesWith3Letters =
                names.stream()
                        .filter(name -> length(name) == 3)
                        .map(BeingLazy_LazyEvaluations::toUpper);
        System.out.println("Stream created, filtered, mapped...");
        System.out.println("ready to call findFirst...");
        final String firstNameWith3Letters =
                namesWith3Letters
                        .findFirst()
                        .orElse("");
        System.out.println(firstNameWith3Letters);
    }

}

Where Next?

Popular Pragmatic Bookshelf topics Top

GilWright
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
jesse050717
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
AleksandrKudashkin
On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New
gilesdotcodes
In case this helps anyone, I’ve had issues setting up the rails source code. Here were the solutions: In Gemfile, change gem 'rails' t...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “&gt;...
New
rainforest
Hi, I’ve got a question about the implementation of PubSub when using a Phoenix.Socket.Transport behaviour rather than channels. Before ...
New
tkhobbes
After some hassle, I was able to finally run bin/setup, now I have started the rails server but I get this error message right when I vis...
New
EdBorn
Title: Agile Web Development with Rails 7: (page 70) I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New
bjnord
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

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
Rainer
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
AstonJ
There’s a whole world of custom keycaps out there that I didn’t know existed! Check out all of our Keycaps threads here: https://forum....
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 ...
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
rustkas
Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New
New
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: