Functional Programming Flashcards
List the 3 paradigms of functional programming?
- Isolated functions - there is no dependence on the state of the program, which includes global variables that are subject to change
- Pure functions - the same input always gives the same output
- 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
In JavaScript, what are callback functions?
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.
In JavaScript, what are first class functions?
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.
In JavaScript what are higher-order functions?
Functions that take a function as an argument, or return a function as a return value.
In JavaScript, what are lambda functions?
Functions that are passed into or returned from another function.
What are the two main reasons functional conding is a good habit?
It keeps your code easy to manage, and saves you from sneaky bugs.
What is the difference between imperative and declarative code?
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)
What is one of the core principles of functional programming in relation to the structure of variables, arrays and objects? Why?
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.
In functional programming what is it called when we change things; and what are the possible outcomes called?
Mutation. Side effects.
What is a pure function?
A function that causes no side effects.
In functional programming, what does “ always declare your dependencies explicitly” principle mean?
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.
In functional programming, what are the benefits of the “explicit dependencies” principle?
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.
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?
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.
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.
What is the main subject matter focus of funtional programming.
A theory of Functions.
In Javascript, functions are part of what class of objects? What does that mean?
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.
Visualise the .map() method being used to return an array of objects.
Visualise the implementation of a .map() method in the Array.prototype object; using a for loop.
In JavaScript, what does the .filter() method do? How many and what arguments does it’s callback function accept?
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.
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.