June 2018
Intermediate to advanced
310 pages
6h 32m
English
A pure function is a function without any side effects. Take the following function, for example:
fun sayHello() { println("Hello")}
How do you test to see whether "Hello" is indeed printed? The task is not as simple as it seems, as we'll need some means to capture the standard output, the same console where we usually see stuff printed.
Compare it to the following function:
fun hello() = "Hello"
The following function doesn't have any side effects. That makes it a lot easier to test:
fun testHello(): Boolean { return "Hello" == hello()}
Does the hello() function look a bit meaningless to your eyes? That's actually one of the properties of pure functions. Their invocation could be replaced by their result (if we knew all their ...
Read now
Unlock full access