
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

When I try the command to create a pair of migration files I get an error.
user=> (create-migration "guestbook")
Execution error (Ill...
New

Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display)
It looks like
.with_simple_console_no_bg(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New

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

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

Is the book’s epub format available to read on Google Play Books?
New

I’m a newbie to Rails 7 and have hit an issue with the bin/Dev script mentioned on pages 112-113.
Iteration A1 - Seeing the list of prod...
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

Title: Agile Web Development with Rails 7: (page 70)
I am running windows 11 pro with rails 7.0.3 and ruby 3.1.2p20 (2022-04-12 revision...
New

I am using Android Studio Chipmunk | 2021.2.1 Patch 2
Build #AI-212.5712.43.2112.8815526, built on July 10, 2022
Runtime version: 11.0....
New
Other popular topics

Reading something? Working on something? Planning something? Changing jobs even!?
If you’re up for sharing, please let us know what you’...
New

Any thoughts on Svelte?
Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New

I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New

I’ve been really enjoying obsidian.md:
It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New

SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
New

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

The V Programming Language
Simple language for building maintainable programs
V is already mentioned couple of times in the forum, but I...
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

Hello everyone! This thread is to tell you about what authors from The Pragmatic Bookshelf are writing on Medium.
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