In OOP, objects/data are the first class citizens.
int result = myFunction(int a); // OOPIn Functional Programming, functions are considered as first class citizens. i.e. functions/behavior can be passed around just like objects/data.
Function aFun = myFunction(Function fun); // Functional ProgrammingThere are 2 characteristics which decide whether a function is a pure function or not:
-
The output of a pure function depends only on
- its input parameters
- its internal algorithm
-
A pure function has no side effects
- A side effect can be anything that a method does beside computing and returning a value
- Any change of instance or class field value is a side effect
- Pure functions always return the same result with the same input
- They do not modify the arguments which are passed to them
// Pure Function public int sum(int a, int b) { return a + b; }
// Impure Function public class ImpureFunction { private int value = 0; public int add(int nextValue) { this.value += nextValue; return this.value; } }
accept()method injava.util.Consumeris an impure function, because it takes one input parameter but returns nothingvoid accept(T t);
Java doc also says the same:
Consumer -> Represents an operation that accepts a single input argument and returns no result. Unlike most other functional interfaces, Consumer is expected to operate via side-effects.
- Benefits of Pure Functions:
- Clarity of thought
- Easy to reason about
- Can be used fearlessly as they never modify the shared state or variables
Higher order functions are the functions which either take functions as arguments or return them as output or do both
.. higherOrderFunction(Function one, Function orMore)
OR
Function higherOrderFunction(..)
OR
Function higherOrderFunction(Function func)In Java we implement functions using Lambda.
So we can say that a Higher order function is a function which either take lambda as input argument or return a lambda or does both.
-
Referential Transparency is a property of a function, variable, or expression whereby the expression can be replaced by its (evaluated) value without affecting the behavior of the program.
-
You should read "Transparent" as "Doesn't make a difference".
-
You could determine the result of applying the function only by looking at the values of its arguments.
-
Pure functions are always Referentially Transparent however a referentially transparent function may not be a pure function.