Functional Programming Flashcards
What is the purpose of Functional programming?
Data separation
Idea of separation
Data and the effects
What are Pure Functions?
A pure function has no side effects on anything outside of it and, given the same input, will always output the same value.
Build lots of minimal, reusable and predictable pure functions that do the following:
• Complete 1 task per function.
• Do not mutate state.
• Do not share state.
• Be predictable.
• Be composable, with one input and one output.
• Be pure if possible.
• Return something.
// Impure:
const array = [1,2,3];
function mutateArray(arr) {
arr.pop()
}
What is Idempotence?
Idempotence is another critical piece of functional programming. It is the idea that given the same input to a function, you will always return the same output.
The function could be used over and over again, and nothing changes. This is how you make your code predictable.
What is Referential transparency?
A critical concept of functional programming is referential transparency, the ability to replace an expression with the resulting value without changing the result of the program.
What difference is Imperative vs Declarative?
Imperative programming tells the computer what to do and how to complete it.
Declarative programming only tells the computer what to do, not how to do things.
// more imperative
for (let i = 0; i < 10; i++) {
}
console.log(i);
// more declarative
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
arr.forEach(item => console.log(item));
What is Immutability?
Immutability is simply not modifying the original data or state. Instead, we should create
copies of the state inside our functions and return a new version of the state.
// Bad code
const obj = {name: ‘Brittney’}
function clone(obj) {
return {…obj} // this is pure
}
obj.name = ‘Joe’ //mutated the state
// Better code
function updateName(obj) {
const newObj = clone(obj)
newObj.name = ‘Joe’
return newObj
}
const updatedNameObj = updateName(obj)
console.log(obj = ${obj}
, `updatedNameObj = ${updatedNameObj})
What is HOF?
Higher order function is a function which returns or takes a parameter as function or both.
What happens during clone with the spread?
A new clone is created
You may be thinking that this could get really expensive, memory-wise, to just copy code over and over. However, there is something called structural sharing that allows the data to only copy new information and points to the original state for any commonalities.
What is structural sharing?
Structural sharing provides an efficient way to share data between multiple versions of it, instead of copying the whole data.
https://dev.to/viebel/structural-sharing-with-7-lines-of-javascript-2dnh
Can closure be a HOF?
Yes, it can be a higher order function
What is currying?
Currying is an advanced technique of working with functions. It’s used not only in JavaScript, but in other languages as well.
Currying is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).
What is partial application?
Partial application is expanding on the idea of currying and taking it a step farther by separating a parameter out.
const multiply = (a, b, c) => a * b * c;
const curriedMultiplyBy5 = multiply.bind(null, 5); // this is null
curriedMultiplyBy5(4, 10); // 200
What is Pipe?
A pipe function is a function that accepts a series of functions, which process an input parameter and return a output which will be the input for the next function.
Example: const pipe = (fn1, fn2) => data => fn2(fn1(data));
What is compose?
It’s just pipe in the other direction.
const compose = (fn1, fn2) => data => fn1(fn2(data));
What is The Pipeline Operator ? |>
// without pipeline operator
double(increment(double(double(5)))); // 42
// with pipeline operator
5 |> double |> double |> increment |> double; // 42