
Introduction to Functional Programming
30 August, 2021
1
1
0
Contributors
In Functional Programing, we want to express our whole program in terms of functions. Functional Programing is Declarative. This means we focus more on ** what to do ** instead of How to do
First, let's understand why Functional Programming is so important. Functional Programming enables us to
- Write re-usable code.
- Debug easily.
- Read better.
Functions are 1st Class Citizens because functions can be :
- Assigned to variables.
- Can be added to objects and arrays as well.
- Sent to other functions as an argument.
- Can be returned from other functions.
Let's get right into it.
** Non-Functional Way**
Functional Way
--> Pure Functions
A Pure Function is a function which, Given the same input, will always return the same output.
A Pure Function :
- Takes in at least 1 parameter.
- Return Something (A value or a function).
- Does not mutate any arguments.
Not Pure
The above code is not pure because
- --> It is not taking name as an parameter.
- --> It's dealing with something in the global scope.
- --> Also it not having a return value.
Pure Functions have no side effects which means it cannot alter anything outside the function.
Pure Function
Higher Order Function
A higher-order function is a function that takes a function as an argument, or returns a function or does both.
Immutable Code
Immutability means can't be changed.
Mutation --> (Bad)
Immutation
Last but not the Least Do not Iterate using for or while/loops --> Use Map, Reduce, Filter etc.
Let me your thoughts.
Hope you found value, and if you did consider supporting me here.
react
javascript
functional programming
function