Functional Programming in JS
What is functional programming?
- Functional programming is programming with pure functions and everything in a functional program is function.
- Functional programming a coding style supported by some languages.
- A program in the functional programming mindset become a function that takes in some kind of input and returns some kind of output.That function itself may be made up of a lot more simple functions that when we put them all together,give us a complex operation worthy of a complete computer program.
note
So the idea with functional programming is that we have inputs coming in,we have outputs coming out.And those outputs can also become new inputs forthe next function whose outputs can become new inputs for another function.
- Pure Functions vs. Side Effects
- Imperative vs. Declarative programming
- Iteration vs. Recursion
- Higher Order Function
- Closures
- Function composition
- Functions as values
- Functions that operate on functions
- Data flow
- Pipelining
- Immutability
- Time, state, and (in)sanity, revisited
- Copying vs. Mutating
- The pitfalls of immutability
Resources
Imperative vs Declarative
Imperative means you tell the compiler what you exactly want to happen, while in declarative paradigm you only specify what you want the result to be.
Declarative style is best followed in functional programming languages while it is pretty fun to follow this style in JavaScript.
- A program as like an imperative, series of commands.
- we have to do this and then do that and then do the other thing.
- It execute line by line.
- follow my commands do this, then that
let name = "Alonzo";
let greeting = "Hi";
console.log(`${greeting}, ${name}!`);
// Hi, Alonzo!
greeting = "Howdy";
console.log(`${greeting}, ${name}!`);
// Howdy, Alonzo!