dtonhofer

dtonhofer

Functional Programming in Java, Second Edition: Chapter 12, p.207 - The key is really mutability

As traditional , the “impure” code of page 207 in the form of a pretend test.

(Doesn’t “impure” refer to performing uncontrolled side effects, like printing to Stdout in the middle of a function though? Here, the lambda of the of the map is really a function of two parameters, one of which is pulled from the context, but it being mutable ruins everything. The key is presence of mutability.

Suppose we have a lambda expression that depends on a field in a class.

That’s okay!

Suppose we have a lambda expression that depends on a mutable (non final) member in a surrounding context.

That’s not okay!

package chapter12;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.stream.Collectors;

public class Impure {

    private int factor = 2; // it's mutable, which is bad (it's not even volatile either)

    @Test
    void impureFunction() {
        final var numbers = List.of(1, 2, 3);
        final var stream = numbers.stream()
                .map(number -> number * factor);
        factor = 0;
        System.out.println(stream.collect(Collectors.toList()));
    }

}

The output here happens to be [0, 0, 0].

0 383 0

Popular Prag Prog topics Top

jamis
The following is cross-posted from the original Ray Tracer Challenge forum, from a post by garfieldnate. I’m cross-posting it so that the...
4 2350 3
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
5 1334 4
New
sdmoralesma
Title: Web Development with Clojure, Third Edition - migrations/create not working: p159 When I execute the command: user=> (create-...
5 1082 2
New
rmurray10127
Title: Intuitive Python: docker run… denied error (page 2) Attempted to run the docker command in both CLI and Powershell PS C:\Users\r...
9 1256 4
New
jskubick
I’m running Android Studio “Arctic Fox” 2020.3.1 Patch 2, and I’m embarrassed to admit that I only made it to page 8 before running into ...
0 1695 3
New
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
5 1393 5
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
0 3929 1
New
adamwoolhether
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...
17 1371 9
New
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
1 2221 1
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
1 1057 10
New

Other popular topics Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
245 5109 101
New
wolf4earth
@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...
193 4292 82
New
Exadra37
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...
227 8401 88
New
AstonJ
SpaceVim seems to be gaining in features and popularity and I just wondered how it compares with SpaceMacs in 2020 - anyone have any thou...
30 3366 14
New
AstonJ
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 ...
10 5387 8
New
mafinar
Crystal recently reached version 1. I had been following it for awhile but never got to really learn it. Most languages I picked up out o...
155 4154 65
New
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
37 3003 14
New
PragmaticBookshelf
Author Spotlight: Bruce Tate @redrapids Programming languages always emerge out of need, and if that’s not always true, they’re defin...
54 4312 23
New
CommunityNews
Will Swifties’ war on AI fakes spark a deepfake porn reckoning?
0 5684 0
New

Latest in PragProg

View all threads ❯