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