Today I am going to talk about how to design Pure Function and help you understand the benefits of pure function.
Nowadays Functional Programming is getting a lot of attention due to the advantages it offers like parallelism, easier testing, predictability and many others.
Pure functions is a concept mainly used in functional programming languages but it can be applied in any programming paradigm
Pure functions can be defined as
If you are a developer, you must come across some common functions we use in programming like strlen(), sqrt(), cos(x) etc. Math.cos(x) , these are
the examples of Pure Function, these functions always returns the same
result for the same value of x
If you wants to write pure function in, pure function might look like this
fun multiply (a: Int,b :Int) = a * b
“If a is 4 and b is 5, the result is always 9, no matter how often or how fast you call it, even if done so concurrently.”
As a counter example, this would be an impure function:
int c = 5; fun multiply (a: Int,b :Int) = a * b * c
In the above example the output depends on the variable c, which is not in scope of pure functionBenefits of pure functions
Pure functions are easier to combine:
It is common practice in FP to combine many smaller pure functions into a simple solution.
In pure functions “output depends only on input”, pure functions are easy to combine together into simple solutions.
For example, you’ll often see FP code written as a chain of function calls, like this:
val x = doThis(a).thendoThis(b) .thendoThis(c) .doThis(d) .andFinallydoThis(e)
Pure functions are easier to test and debug
Since pure function only operates on input values, there is no other
dependency hence it makes pure functions very easy to test, as we only
need to test that the inputs produce the desired outputs. We do not need
to check the validity of any global program state in our tests of
Pure functions are easier to debug than impure functions. Because the output of a pure function depends only on the function’s input parameters and your algorithm, you don’t need to look outside the function’s scope to
Pure functions are easier to parallelize
Pure functions are easier to parallelize. You can easily distribute the
input values over a number of threads, and collect the results.
An expression is said to be referentially transparent if it can be
replaced with its corresponding value without changing the program’s
To achieve referential transparency, a function must be pure. This has
benefits in terms of readability and speed. Compilers are often able to
optimize code that exhibits referential transparency.
Caching and reusing the result of a computation is called memoization, and can only be done safely with pure functions.
Because a pure function always returns the same result when given the same inputs, a compiler (or your application) can also use caching
optimizations, such as memoization.
Laziness is a major feature of functional programming. We only ever need to compute the result of a pure function once, but what if we can avoid the computation entirely? What if you never use the output value of pure function?
As the function can not cause side effects, it does not matter if it is called or not. You can write a program to be lazy and optimize the call to pure functions away.
Also published on: https://firstname.lastname@example.org/design-pure-functions-a2b76c68666e
Create your free account to unlock your custom reading experience.