
dtonhofer
Functional Programming in Java, Second Edition: Chapter 12
On page 200, the code can use boxed()
instead of the (somewhat mysterious) maptoObject(e -> e)
One should also reveal to the reader that we are computing perfect numbers
package chapter12;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.LongStream;
public class Chapter12 {
// Confusing code on page 200
// but with mapToObj(e -> e) replaced with boxed()
private static List<Long> confusingCode(long number) {
return LongStream.rangeClosed(1, number)
.filter(i -> { //Bad Code, don't do this
long factor = 0;
for (int j = 1; j < i; j++) {
if (i % j == 0) {
factor += j;
}
}
return factor == i;
})
.boxed()
.toList();
}
// Confusing code on page 201
// but with mapToObj(e -> e) replaced with boxed()
private static List<Long> refactoredConfusingCode(long number) {
return LongStream.range(1, number)
.filter(i -> LongStream.range(1, i) //Not good
.filter(j -> i % j == 0)
.sum() == i)
.boxed()
.toList();
}
// Nonconfusing code on page 201, in two methods
private static Long sumOfDivisors(long number) {
return LongStream.range(1, number)
.filter(i -> number % i == 0)
.sum();
}
// Create list of those x <= limit such that the sum of the divisors of x equals x
// https://en.wikipedia.org/wiki/Perfect_number
// https://oeis.org/A000396
private static List<Long> nonConfusingCode(long limit) {
return LongStream.rangeClosed(1, limit)
.filter(i -> sumOfDivisors(i) == i)
.boxed()
.toList();
}
// A record to pass data along the stream
private record InAndOut(Long in, List<Long> out) {
public String toString() {
return "f(" + in + ")=[" + out.stream().map(it -> Long.toString(it)).collect(Collectors.joining(",")) + "]";
}
}
private static String exerciseCode(Function<Long, List<Long>> f, List<Long> input) {
return input.stream()
.map(it -> new InAndOut(it, f.apply(it)))
.map(InAndOut::toString)
.collect(Collectors.joining(", "));
}
@Test
public void exerciseAllCode() {
var input = List.of(0L, 1L, 2L, 10L, 13L, 100L, 1000L, 10000L);
{
String str = exerciseCode(Chapter12::confusingCode, input);
System.out.println(str);
}
{
String str = exerciseCode(Chapter12::refactoredConfusingCode, input);
System.out.println(str);
}
{
String str = exerciseCode(Chapter12::nonConfusingCode, input);
System.out.println(str);
}
}
}
Popular Pragmatic Bookshelf topics

This isn’t directly about the book contents so maybe not the right forum…but in some of the code apps (e.g. turbo/06) it sends a TURBO_ST...
New

Title: Intuitive Python: docker run… denied error (page 2)
Attempted to run the docker command in both CLI and Powershell
PS C:\Users\r...
New

Hello! Thanks for the great book.
I was attempting the Trie (chap 17) exercises and for number 4 the solution provided for the autocorre...
New

This is as much a suggestion as a question, as a note for others.
Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New

“The ProductLive.Index template calls a helper function, live_component/3, that in turn calls on the modal component. ”
Excerpt From: Br...
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

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

Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New

The allprojects block listed on page 245 produces the following error when syncing gradle:
“org.gradle.api.GradleScriptException: A prob...
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
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

I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
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

This looks like a stunning keycap set :orange_heart:
A LEGENDARY KEYBOARD LIVES ON
When you bought an Apple Macintosh computer in the e...
New

Think Again 50% Off Sale »
The theme of this sale is new perspectives on familiar topics.
Enter coupon code ThinkAgain2021 at checkout t...
New

Create efficient, elegant software tests in pytest, Python's most powerful testing framework.
Brian Okken @brianokken
Edited by Kat...
New

The File System Access API with Origin Private File System.
WebKit supports new API that makes it possible for web apps to create, open,...
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
Erin Dees
@undees
Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New

Fight complexity and reclaim the original spirit of agility by learning to simplify how you develop software. The result: a more humane a...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /emacs
- /clojure
- /haskell
- /java
- /onivim
- /typescript
- /svelte
- /crystal
- /kotlin
- /c-plus-plus
- /tailwind
- /gleam
- /react
- /ocaml
- /flutter
- /elm
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /deepseek
- /zig
- /scala
- /sublime-text
- /textmate
- /lisp
- /nixos
- /debian
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /manjaro
- /spring
- /nodejs
- /diversity
- /lua
- /julia
- /slackware
- /c
- /neovim