5 - Higher-Order Functions Flashcards

1
Q

How to collect names from [{ id: 1, name: ‘John’ }, { id: 2, name: ‘Mark’ }]?

A

Array.map

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

If you had this data structure [{ id: 1, name: ‘John’ }, { id: 2, name: ‘Mark’ }]. How would output names on a console? Which function would you use?

A

Array.forEach

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

How to sum up counts in [{ id: 1, count: 2 }, { id: 2, count: 13 }]?

A

Array.reduce

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

What is the name of the concept that allows us to hide implementation details and create a vocabulary of our own to discuss the code?

A

abstractions

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

What are abstractions useful for?

A
  1. constructing domain concepts specific to the domain we are solving a problem for
  2. moving implementation details into background
  3. writing separate pieces of code which we can test in isolation
  4. creating vocabulary within our program
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why is building a vocabulary a powerful idea?

A

Because a custom vocabulary enables you to reason about a program more efficiently.

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

Which programming construct has these two characteristics:
1. they take functions as an argument
2. they return a function as a return value

A

Higher-order function.

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

When processing very large data structures, is it usually faster to use functions behind abstractions or process the data without any abstractions?

A

Process the data without abstractions. It is faster because usually lesser number of objects are created, and lesser number of operations are performed.

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

What will this pure function return?

function foo(ary) {
return ary.push(1);
}

A

This is not a pure function because it is mutating the array it receives in the param.

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

How can you use a function value within a HOF?

A
  1. you can call it to make a computation
  2. you can pass it as an argument to another function
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How can you leverage closure when working with HOFs?

A

You can return a function from HOF, which means that each returned function will have access to the scope within the HOF it was created in. That way they can be customized with the arguments passed to HOF and used to build functions HOFs return as their return value.

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

What does it mean for a function to be pure?

A

1 - it doesn’t mutate arguments passed to it. Instead it creates new objects for its own purposes
2 - given the same arguments it always returns the same value.

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

What is the name of the function which allows you to select only certain elements from an array?

A

Array.filter

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

What is a Han unification?

A

It’s a collection of characters shared by Japanese, Korean and some Chinese dialect which was created because some of the characters between these languages overlaped.

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

How is the ability to use different HOF for processing data called?

A

Composability

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

What is a code unit?

A

Number that corresponds to a Javascript character.

15
Q

How are JS strings encoded?

A

As 16-bit numbers.

16
Q

How many characters Unicode provides?

A

Around 65 000.

17
Q

How many numbers does a 16-bit sequence support?

A

Around 65 000.

18
Q

Why was UTF-16 created?

A

Because the community didn’t want to assign additional numbers to characters in the scripts whose characters were not included in the Unicode character set.

19
Q

Instead of using more numbers to represent different characters, how does UTF-16 solve that problem?

A

It assigns two code units to characters outside of Unicode char set.

20
Q

What does the argument given to codePointAt retrieve?

A

Full Unicode character.

21
Q

How can you run through the sequence of characters in the string without worrying to check if the value under an index is a valid character, or one half of a valid character?

A

With for/of loop.

22
Q

Why does for/of loop work on strings?

A

Because they’re iterable.

23
Q

What does String.length count in the string?

A

Number of code units.

24
Q

If you have a array and want to find an index of an element that satisfies a certain condition, which method you need to use?

A

Array.findIndex

25
Q

If you have a array and want to retrieve first element that satisfies a certain condition, which method you need to use?

A

Array.find