1. λ pure functions by Per Arneng 2015-02-08
pure
functions
pure
functions
2. λ pure functions by Per Arneng 2015-02-08
pure functions
● It always evaluates the same result value given
the same argument value(s)
● The result value cannot depend on any hidden
information or state that may change as
program execution proceeds or between
different executions of the program
● It can not depend on any external input from I/O
devices
pj f ŋk nsʊɚ ə ʃə
3. λ pure functions by Per Arneng 2015-02-08
pure functions
● It always evaluates the same result value given
the same argument value(s)
● The result value cannot depend on any hidden
information or state that may change as
program execution proceeds or between
different executions of the program
● It can not depend on any external input from I/O
devices
pj f ŋk nsʊɚ ə ʃə
4. λ pure functions by Per Arneng 2015-02-08
pure functions
● It always evaluates the same result value given
the same argument value(s)
● The result value cannot depend on any hidden
information or state that may change as
program execution proceeds or between
different executions of the program
● It can not depend on any external input from I/O
devices
pj f ŋk nsʊɚ ə ʃə
5. λ pure functions by Per Arneng 2015-02-08
ex:
int addOne(int value) {
return value + 1;
}
6. λ pure functions by Per Arneng 2015-02-08
impure:
void addOne(int *value) {
*value = *value = 1;
}
7. λ pure functions by Per Arneng 2015-02-08
impure:
void addOne(int *value) {
*value = *value = 1;
}
The argument is mutated
8. λ pure functions by Per Arneng 2015-02-08
impure 2:
int addOne(int value) {
int newValue = value + 1;
file.write(newValue);
return newValue;
}
9. λ pure functions by Per Arneng 2015-02-08
impure 2:
int addOne(int value) {
int newValue = value + 1;
file.write(newValue);
return newValue;
}
Could fail making the function
non deterministic and impure
10. λ pure functions by Per Arneng 2015-02-08
impure 2:
int addOne(int value) {
int newValue = value + 1;
file.write(newValue);
return newValue;
}
Using a hidden state
11. λ pure functions by Per Arneng 2015-02-08
advantages
● Easy to test
● Easy to reuse
● Easy to reason about (read, understand)
● Perfect for use in multithreading
12. λ pure functions by Per Arneng 2015-02-08
testability
● You do not need to mock hidden state resulting
in smaller and faster tests
● You only need to focus and arguments and
return values
● Tests that are not mocked are executed faster
than mocked ones. For hundreds of tests it will
matter
13. λ pure functions by Per Arneng 2015-02-08
maintainability
● Usually smaller blocks of code that can just be
copied out and reused in another context
● Easy to understand and reason about so you
are less likely to feel that you need to write your
own
● Easy to use as building blocks for other
functions and methods as inline or passed as
parameters
14. λ pure functions by Per Arneng 2015-02-08
readability
● When a pure function is used you know that it
will not modify anything making it easier to read
that code
● When you read a pure function your brain does
not have to keep external state in mind
15. λ pure functions by Per Arneng 2015-02-08
multithreading
● A pure function does not mutate it's argument
or make use of any hidden state so its easier to
use in a multithreaded context without having to
resort to synchronization
16. λ pure functions by Per Arneng 2015-02-08
more
● https://github.com/PerArneng/pfnomock
A repo showing a pure and non pure function
for handling transfers between accounts and
what that means in terms of testing with and
without mocking