Declarative style describes how something is done; step by step. In imperative style (object oriented programming), we describe how something can be done; define what that something is. In declarative styles, we define what the something is and how to calculate the average.
Immutability
Variables are just another name for their values in Funcational Programming
- A variable once set remains unchanged in functional programming
- The same way, every x, y, z in your program can now have a single value
- Example: Employee john = new Employee(“John”, “10$”)
- Employee updatedJohn = new Employe(new Employee, “20$”)
Partial Application
In partial application, the three arguments are provided at different times.
Composition
Take simple functions and combine them into a more complex function.
- The requirement here is that the next function takes as argument the same type that the previous function returns.Functional programming and Object-oriented programming are not opposites and can exist together
Functional Purity
If a function returns same value for the same argument every time, then the function is pure
- This generally means that, the function cannot use any state values that could potentially change
- An example of an impure function
- int add(int x, int y) { return x + y; }
- All the logic for computation involves only the inputs and the variables declared inside the function
First Class Functions
In OOP, data and functions are treated separately, but in FP, they are all the same
- Create an array of functions, pass a function as an argument, and return a function from a method
- Java provides a Function interface to do this
- Only works for functions with 1 argument
- BiFunction interface for functions that take 2 arguments
- For anything more, you can define your own interface
- Example: ThreeArgFunction