
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

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

Hi Jamis,
I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019.
This test doesn’t pass for me:
...
New

I am working on the “Your Turn” for chapter one and building out the restart button talked about on page 27. It recommends looking into ...
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

“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ”
Excerpt From: Br...
New

When trying to run tox in parallel as explained on page 151, I got the following error:
tox: error: argument -p/–parallel: expected one...
New

Hi,
I completed chapter 6 but am getting the following error when running:
thread 'main' panicked at 'Failed to load texture: IoError(O...
New

Book: Programming Phoenix LiveView, page 142 (157/378), file lib/pento_web/live/product_live/form_component.ex, in the function below:
d...
New

Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
Other popular topics

A thread that every forum needs!
Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
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

I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me.
I think the Planck and the Preonic (o...
New

Do the test and post your score :nerd_face:
:keyboard:
If possible, please add info such as the keyboard you’re using, the layout (Qw...
New

Seems like a lot of people caught it - just wondered whether any of you did?
As far as I know I didn’t, but it wouldn’t surprise me if I...
New

Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New

Intensively researching Erlang books and additional resources on it, I have found that the topic of using Regular Expressions is either c...
New

If you get Can't find emacs in your PATH when trying to install Doom Emacs on your Mac you… just… need to install Emacs first! :lol:
bre...
New
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
- /wasm
- /ruby
- /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
- /html
- /php
- /deepseek
- /zig
- /scala
- /lisp
- /textmate
- /sublime-text
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /manjaro
- /spring
- /nodejs
- /diversity
- /lua
- /slackware
- /c
- /julia
- /markdown