Functional Programming Flashcards

1
Q

Functional programming follows a few core principles:

A

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

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

Pure functions mean

A

the same input always gives the same output

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

Callbacks are

A

the functions that are slipped or passed into another function to decide the invocation of that function.

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

First class functions are

A

Functions that can be assigned to a variable,

Passed into another function, or

Returned from another function just like any other normal value

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

In JavaScript, all functions are _____ functions.

A

first class

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

The functions that take a function as an argument, or return a function as a return value are called ____

A

higher order functions.

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

Lambdas are

A

the functions are passed in to another function or returned from another function – those functions which gets passed in or returned

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

an imperative style in programming is

A

one that gives the computer a set of statements to perform a task.

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

functional programming is a form of ____

A

declarative programming. You tell the computer what you want done by calling a method or function.

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

principle of functional programming is to ____

A

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

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

map(({Title: title, imdbRating: rating}) => ({title, rating})) //does something

A

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.

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

filter(obj => obj.property < value) //does

A

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.

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

using concat() vs push()

A

concat doesn’t mutate the original, but push does

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

sort by ascending order using arr.sort() (code)

A
function ascendingOrder(arr) {
  return arr.sort(function(a, b) {
    return a - b;
  });
}
ascendingOrder([1, 5, 2, 3, 4]);
// Returns [1, 2, 3, 4, 5]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

JavaScript’s default sorting method is by

A

string Unicode point value

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

Logic of how compareFunction (callback for sort()) works

A

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>

17
Q

A side effect of the sort method is that it ___

A

changes the order of the elements in the original array

18
Q

One way to avoid sort() mutating the original array is to first ___

A

concatenate an empty array to the one being sorted (remember that slice and concat return a new array), then run the sort method.

19
Q

the join() method

A

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.

20
Q

every() method

A

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.

21
Q

some() method

A

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.

22
Q

The arity of a function is

A

the number of arguments it requires.

23
Q

Currying a function means

A

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

24
Q

Example code of uncurried and curried functions

A
//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

25
Q

Currying is useful in your program if

A

you can’t supply all the arguments to a function at one time.

You can save each function call into a variable, which will hold the returned function reference that takes the next argument when it’s available. Here’s an example using the curried function in the example above:

// Call a curried function in parts:
var funcForY = curried(1);
console.log(funcForY(2)); // Prints 3
26
Q

partial application can be described as

A

applying a few arguments to a function at a time and returning another function that is applied to more arguments. Here’s an example:

//Impartial function
function impartial(x, y, z) {
  return x + y + z;
}
var partialFn = impartial.bind(this, 1, 2);
partialFn(10); // Returns 13