Functional Programming Flashcards

1
Q

List the 3 paradigms of functional programming?

A
  1. Isolated functions - there is no dependence on the state of the program, which includes global variables that are subject to change
  2. Pure functions - the same input always gives the same output
  3. Functions with limited side effects - any changes, or mutations, to the state of the program outside the function are carefully controlled.

This is a style of programming where solutions are simple, isolated functions, without any side effects outside of the function scope: INPUT -> PROCESS -> OUTPUT

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

In JavaScript, what are callback functions?

A

Functions that are slipped or passed into another function to decide the invocation of that function. You may have seen them passed to other methods, for example in filter, the callback function tells JavaScript the criteria for how to filter an array.

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

In JavaScript, what are first class functions?

A

Functions that can be assigned to a variable, passed into another function, or returned from another function just like any other normal value.

In JavaScript, all functions are first class functions.

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

In JavaScript what are higher-order functions?

A

Functions that take a function as an argument, or return a function as a return value.

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

In JavaScript, what are lambda functions?

A

Functions that are passed into or returned from another function.

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

What are the two main reasons functional conding is a good habit?

A

It keeps your code easy to manage, and saves you from sneaky bugs.

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

What is the difference between imperative and declarative code?

A

Imperative tells the computer what to do and changes the state of the program (e.g. for loop) while declarative tells the computer what to do by calling a method or function (e.g. in stead of a for loop it uses the .map() method and avoids the pitfalls of the for loop)

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

What is one of the core principles of functional programming in relation to the structure of variables, arrays and objects? Why?

A

Do not change things. Changes lead to bugs. It’s easier to prevent bugs knowing that your functions don’t change anything, including the function arguments or any global variable.

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

In functional programming what is it called when we change things; and what are the possible outcomes called?

A

Mutation. Side effects.

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

What is a pure function?

A

A function that causes no side effects.

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

In functional programming, what does “ always declare your dependencies explicitly” principle mean?

A

It means that if a function depends on a variable or object being present, then pass that variable or object directly into the function as an argument.

Juche.

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

In functional programming, what are the benefits of the “explicit dependencies” principle?

A

The function is easier to test, you know exactly what input it takes, and it won’t depend on anything else in your program.

This can give you more confidence when you alter, remove, or add new code. You would know what you can or cannot change and you can see where the potential traps are.

Finally, the function would always produce the same output for the same set of inputs, no matter what part of the code executes it.

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

What would happen if you declared a function that has const “newArr = arrVar”, where arrVar is an array outside the function? Where is this a most important consideration?

A

It will simply create a reference to the existing variable and not a copy. So changing a value in newArr would change the value in arrVar.

This is very important in functional programming.

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

Visualise 2 functions, in funtional programming, that add and remove items (accepted as paratemeter) from an array (accepted as a parameter) without changing that array.

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

What is the main subject matter focus of funtional programming.

A

A theory of Functions.

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

In Javascript, functions are part of what class of objects? What does that mean?

A

They are first class objects. That means that they can be used like any other object.

Foe example, they can be saved in variables, stored in an object, or passed as function arguments.

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

Visualise the .map() method being used to return an array of objects.

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

Visualise the implementation of a .map() method in the Array.prototype object; using a for loop.

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

In JavaScript, what does the .filter() method do? How many and what arguments does it’s callback function accept?

A

It calls a function on each element of an array and returns a new array containing only the elements for which that function returns true. It does this without changing the original data.

The callback function accepts three arguments. The first argument is the current element being processed. The second is the index of that element and the third is the array upon which the filter method was called.

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

Use a combination of filter and map on watchList to assign a new array of objects with only title and rating keys. The new array should only include objects where imdbRating is greater than or equal to 8.0. Note that the rating values are saved as strings in the object and you may need to convert them into numbers to perform mathematical operations on them.

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

Write your own Array.prototype.myFilter(), which should behave exactly like Array.prototype.filter(). You should not use the built-in filter method. The Array instance can be accessed in the myFilter method using this.

A
22
Q

In Javascript, what happens when you don’t pass any arguments into the .slice() method?

A

It performs on the whole array? It is an easy way to copy an array without modifying it.

23
Q

In Javascript, what does the .concat() method do?

A
24
Q

In Javascript, what is functional programming’s response to the .push() method? Why?

A

the .concat() method, because it adds new elements to an array without mutating the original array i.e. produces a copy and then adds the new elements.

25
Q

Visualise JS code that uses the .reduce() method to return the sum of all the ages from the object below:

A
26
Q

Visualise JS code that uses the .reduce() method to return an object containing the names of the users as properties with their ages as values; from the below object.

A
27
Q

The variable watchList holds an array of objects with information on several movies. Use map, filter and reduce to find the average IMDB rating of the movies directed by Christopher Nolan.

A
28
Q

Use any combination of map(), filter(), and reduce() to return a new array containing the squares of only the positive integers (decimal numbers are not integers) when an array of real numbers is passed to it. An example of an array of real numbers is [-3, 4.8, 5, 3, -3.2].

A
29
Q

In Javascript, what does the .sort() method do?

A

It sorts an array according to the logic of it’s callback function.

30
Q

Visualise a JS function that takes an array as a parameter and returns that array in ascending order.

A
31
Q

Visualise a JS function that takes an array as a parameter and returns that array in reverse order using the .sort() method.

A
32
Q

In Javascrip, why is it encouraged that a callback function be provided to the .sort() method?

What is such a callback function called normally? Explain how this function works.

A

Because JavaScript’s default sorting method is by string Unicode point value, which may return unexpected results.

The callback function for the .sort() method is normally called compareFunction.

When such a callback function is supplied, the array elements are sorted according to the return value of the compareFunction: If compareFunction(a,b) returns a value less than 0 for two elements a and b, then a will come before b. If compareFunction(a,b) returns a value greater than 0 for two elements a and b, then b will come before a. If compareFunction(a,b) returns a value equal to 0 for two elements a and b, then a and b will remain unchanged.

33
Q

Visualise a JS function that takes an array as a parameter and returns that array in alphabetical order using the .sort() method.

A
34
Q

Use the sort method in Javascript to sort the elements of an array in ascending order. The function should return a new array, and not mutate the globalArray variable.

A
35
Q

Explain in detail what the .split() method does in JavaScript.

A

The split method splits a string into an array of strings. It takes an argument for the delimiter, which can be a character to use to break up the string or a regular expression. For example, if the delimiter is a space, you get an array of words, and if the delimiter is an empty string, you get an array of each character in the string.

36
Q

Explain in detail what the .join() method does in JavaScript.

A

It is the reverse of the .split() method.

The join method is used to join the elements of an array together to create a string. It takes an argument for the delimiter that is used to separate the array elements in the string.

37
Q

Use the join method (among others) inside the sentensify function to make a sentence from the words in the string str. The function should return a string. For example, I-like-Star-Wars would be converted to I like Star Wars. For this challenge, do not use the replace method.

A
38
Q
A
39
Q

What does the .every() method do? What does it return?

A

It works with arrays to check if every element passes a particular test. It returns a Boolean value - true if all values meet the criteria, false if not.

40
Q

Use the every method inside the checkPositive function to check if every element in arr is positive. The function should return a Boolean value.

A
41
Q

What does the .some() method do? What does it return?

A

It works with arrays to check if any element passes a particular test. It returns a Boolean value - true if any of the values meet the criteria, false if not.

42
Q

Use the some method inside the checkPositive function to check if any element in arr is positive. The function should return a Boolean value.

A
43
Q

What is the arity of a function?

A

The number of arguments it requires.

44
Q

What does it mean to curry a function? Visualise an example.

A

It means converting a function of N arity into N functions of arity 1.

45
Q

In Javascript, what is partial application?

A
46
Q

Fill in the body of a function “add” which takes “x” as its argument and uses currying to add parameters x, y, and z.

A
47
Q

What is the Arguments object?

A

arguments is an Array-like object accessible inside functions that contains the values of the arguments passed to that function.

Note: If you’re writing ES6 compatible code, then rest (e.g.: (arg1, …args)) parameters should be preferred.

48
Q

What does Array-like mean?

A

It means that arguments has a length property and properties indexed from zero, but it doesn’t have Array’s built-in methods like forEach() or map().

49
Q

What is the Object.values() method?

A

This method returns an array of a given object’s own enumerable property values, in the same order as that provided by a for…in loop. (The only difference is that a for…in loop enumerates properties in the prototype chain as well.)

50
Q

What is the .includes() method?

A

This method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

51
Q

What does the Object.keys() method do?

A

It returns an array of a given object’s own enumerable property names, iterated in the same order that a normal loop would.