Functional Programming Flashcards

1
Q

What is the purpose of Functional programming?

A

Data separation
Idea of separation
Data and the effects

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are Pure Functions?

A

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()
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is Idempotence?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is Referential transparency?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What difference is Imperative vs Declarative?

A

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));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is Immutability?

A

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})

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is HOF?

A

Higher order function is a function which returns or takes a parameter as function or both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens during clone with the spread?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is structural sharing?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Can closure be a HOF?

A

Yes, it can be a higher order function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is currying?

A

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).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is partial application?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is Pipe?

A

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));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is compose?

A

It’s just pipe in the other direction.

const compose = (fn1, fn2) => data => fn1(fn2(data));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is The Pipeline Operator ? |>

A

// without pipeline operator
double(increment(double(double(5)))); // 42
// with pipeline operator

5 |> double |> double |> increment |> double; // 42

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is Arity?

A

Arity simply means the number of arguments a function takes. The more parameters a function has, the harder it becomes to break apart and reuse. Try to stick to only 1 or 2 parameters when writing functions.

17
Q

What is Composition?

A

Composition is we do with functional programming, creating small reusable function to make code modular.

18
Q

What are the drawbacks for OOPS?

A

One of the drawbacks to inheritance is that it is based on the fact that it won’t change, we tell it what it is. We create a class and give it properBes and methods that describe the class. But say, down the road, we need to update that class and add more funcBonality. Adding a new method to the base class will create rippling effects through your enBre program. FP is more declaraBve, what to do not how, and OOP is more imperaBve, what and how to do something. This is the Aght coupling problem, things having to depend on one another, which leads to the fragile base class problem, seemingly safe changes cause unforeseen repercussions. It is the opposite of small reusable code. Changing one small thing in either of the class or subclasses could break the program. Another problem is hierarchy where you may need to create a subclass that can only do 1 part of the class, but instead you get everything passed down to it.