Functional Programming Flashcards
Functional programming follows a few core principles:
Functions are independent from the state of the program or global variables. They only depend on the arguments passed into them to make a calculation
Functions try to limit any changes to the state of the program and avoid changes to the global objects holding data
Functions have minimal side effects in the program
Pure functions mean
the same input always gives the same output
Callbacks are
the functions that are slipped or passed into another function to decide the invocation of that function.
First class functions are
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 _____ functions.
first class
The functions that take a function as an argument, or return a function as a return value are called ____
higher order functions.
Lambdas are
the functions are passed in to another function or returned from another function – those functions which gets passed in or returned
an imperative style in programming is
one that gives the computer a set of statements to perform a task.
functional programming is a form of ____
declarative programming. You tell the computer what you want done by calling a method or function.
principle of functional programming is to ____
always declare your dependencies explicitly.
This means if a function depends on a variable or object being present, then pass that variable or object directly into the function as an argument
map(({Title: title, imdbRating: rating}) => ({title, rating})) //does something
iterates over each item in an array
Returns: new array containing the results of calling the callback function on each element.
The original array doesn’t change.
filter(obj => obj.property < value) //does
calls a function on each element of an array
Returns: a new array containing only the elements for which that function returns true
The original array doesn’t change.
In other words, it filters the array, based on the function passed to it.
using concat() vs push()
concat doesn’t mutate the original, but push does
sort by ascending order using arr.sort() (code)
function ascendingOrder(arr) { return arr.sort(function(a, b) { return a - b; }); } ascendingOrder([1, 5, 2, 3, 4]); // Returns [1, 2, 3, 4, 5]
JavaScript’s default sorting method is by
string Unicode point value
Logic of how compareFunction (callback for sort()) works
less than 0 for two elements a and b, then a will come before b.
greater than 0 for two elements a and b, then b will come before a.
0 for two elements a and b, then a and b will remain unchanged.
return arr.sort((a,b) => a === b ? 0: a<b></b>
A side effect of the sort method is that it ___
changes the order of the elements in the original array
One way to avoid sort() mutating the original array is to first ___
concatenate an empty array to the one being sorted (remember that slice and concat return a new array), then run the sort method.
the join() method
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.
every() method
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.
some() method
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.
The arity of a function is
the number of arguments it requires.
Currying a function means
to convert a function of N arity into N functions of arity 1.
it restructures a function so it takes one argument, then returns another function that takes the next argument, and so on
Example code of uncurried and curried functions
//Un-curried function function unCurried(x, y) { return x + y; }
//Curried function function curried(x) { return function(y) { return x + y; } } //Alternative using ES6 const curried = x => y => x + y
curried(1)(2) // Returns 3