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
In Chapter 3, the source for index introduces Config on page 31, followed by more code including tests; Config isn’t introduced until pag...
New
Python Testing With Pytest - Chapter 2, warnings for “unregistered custom marks”
While running the smoke tests in Chapter 2, I get these...
New
Title: Hands-on Rust: question about get_component (page 295)
(feel free to respond. “You dug you’re own hole… good luck”)
I have somet...
New
Hi,
build fails on:
bracket-lib = “~0.8.1”
when running on Mac Mini M1 Rust version 1.5.0:
Compiling winit v0.22.2
error[E0308]: mi...
New
Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64)
If you follow the defau...
New
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
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 “>...
New
Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New
Hi, I’m working on the Chapter 8 of the book.
After I add add the point_offset, I’m still able to see acne:
In the image above, I re...
New
From page 13:
On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New
Other popular topics
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it.
Does your monitor have eye p...
New
New
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
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
Just done a fresh install of macOS Big Sur and on installing Erlang I am getting:
asdf install erlang 23.1.2
Configure failed.
checking ...
New
Create efficient, elegant software tests in pytest, Python's most powerful testing framework.
Brian Okken @brianokken
Edited by Kat...
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
Erin Dees
@undees
Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New
There appears to have been an update that has changed the terminology for what has previously been known as the Taskbar Overflow - this h...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /java
- /haskell
- /svelte
- /onivim
- /typescript
- /kotlin
- /c-plus-plus
- /crystal
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /html
- /opensuse
- /zig
- /centos
- /deepseek
- /php
- /scala
- /react-native
- /sublime-text
- /lisp
- /textmate
- /debian
- /nixos
- /agda
- /django
- /kubuntu
- /deno
- /arch-linux
- /nodejs
- /ubuntu
- /revery
- /manjaro
- /spring
- /diversity
- /lua
- /markdown
- /julia
- /slackware








