
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 test is broken right out of the box…
— FAIL: TestAgent (7.82s)
agent_test.go:77:
Error Trace: agent_test.go:77
agent_test.go:...
New

Title: Hands-On Rust (Chapter 11: prefab)
Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New

I thought that there might be interest in using the book with Rails 6.1 and Ruby 2.7.2. I’ll note what I needed to do differently here.
...
New

Hi @Margaret ,
On page VII the book tells us the example and snippets will be all using Elixir version 1.11
But on page 3 almost the en...
New

Page 28: It implements io.ReaderAt on the store type.
Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt?
...
New

First, the code resources:
Page 237: rumbl_umbrella/apps/rumbl/mix.exs
Note: That this file is missing.
Page 238: rumbl_umbrella/app...
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

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

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

@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

@AstonJ prompted me to open this topic after I mentioned in the lockdown thread how I started to do a lot more for my fitness.
https://f...
New
New

poll
poll
Be sure to check out @Dusty’s article posted here: An Introduction to Alternative Keyboard Layouts It’s one of the best write-...
New

Thanks to @foxtrottwist’s and @Tomas’s posts in this thread: Poll: Which code editor do you use? I bought Onivim! :nerd_face:
https://on...
New

Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New

“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco
Ever wonder how authoring books compares to writing articles?...
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

Author Spotlight
Erin Dees
@undees
Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
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

Background
Lately I am in a quest to find a good quality TTS ai generation tool to run locally in order to create audio for some videos I...
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
- /c-plus-plus
- /kotlin
- /tailwind
- /gleam
- /ocaml
- /react
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /zig
- /scala
- /sublime-text
- /textmate
- /debian
- /nixos
- /lisp
- /react-native
- /agda
- /kubuntu
- /arch-linux
- /django
- /revery
- /ubuntu
- /spring
- /manjaro
- /diversity
- /nodejs
- /lua
- /slackware
- /julia
- /c
- /neovim