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

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...
New

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

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

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...
New

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...
New

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...
New

On page 78 the following code appears:
<%= link_to ‘Destroy’, product,
class: ‘hover:underline’,
method: :delete,
data: { confirm...
New

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 ...
New

@mfazio23
Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New
Other popular topics

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...
New

Inspired by this post from @Carter, which languages, frameworks or other tech or tools do you think is killing it right now? :upside_down...
New

Biggest jackpot ever apparently! :upside_down_face:
I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New

Author Spotlight
Dmitry Zinoviev
@aqsaqal
Today we’re putting our spotlight on Dmitry Zinoviev, author of Data Science Essentials in ...
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

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...
New

Author Spotlight
Mike Riley
@mriley
This month, we turn the spotlight on Mike Riley, author of Portable Python Projects. Mike’s book ...
New

Author Spotlight:
Bruce Tate
@redrapids
Programming languages always emerge out of need, and if that’s not always true, they’re defin...
New

This is cool!
DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON
We just witnessed something incredible: the largest open-s...
New

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...
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