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);
    }

}
0 226 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
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
5 1334 4
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...
3 1274 2
New
patoncrispy
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...
5 1484 3
New
curtosis
Running mix deps.get in the sensor_hub directory fails with the following error: ** (Mix) No SSH public keys found in ~/.ssh. An ssh aut...
2 1442 3
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
0 7996 6
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
5 1406 5
New
hazardco
On page 78 the following code appears: &lt;%= link_to ‘Destroy’, product, class: ‘hover:underline’, method: :delete, data: { confirm...
1 1265 2
New
creminology
Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
6 1112 5
New
dachristenson
@mfazio23 Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
0 2099 5
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
144 8502 50
New
AstonJ
Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
160 3807 49
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
19 3178 10
New
New
AstonJ
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...
4 3745 1
New
Help
I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
8 3528 3
New
PragmaticBookshelf
Author Spotlight Mike Riley @mriley This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
62 6351 19
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
54 4591 23
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
0 3570 1
New
AstonJ
This is a very quick guide, you just need to: Download LM Studio: https://lmstudio.ai/ Click on search Type DeepSeek, then select the o...
14 5193 10
New

Latest in Functional Programming in Java, Second Edition

Functional Programming in Java, Second Edition Portal

Sub Categories: