5 - Higher-Order Functions Flashcards
How to collect names from [{ id: 1, name: ‘John’ }, { id: 2, name: ‘Mark’ }]?
Array.map
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?
Array.forEach
How to sum up counts in [{ id: 1, count: 2 }, { id: 2, count: 13 }]?
Array.reduce
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?
abstractions
What are abstractions useful for?
- constructing domain concepts specific to the domain we are solving a problem for
- moving implementation details into background
- writing separate pieces of code which we can test in isolation
- creating vocabulary within our program
Why is building a vocabulary a powerful idea?
Because a custom vocabulary enables you to reason about a program more efficiently.
Which programming construct has these two characteristics:
1. they take functions as an argument
2. they return a function as a return value
Higher-order function.
When processing very large data structures, is it usually faster to use functions behind abstractions or process the data without any abstractions?
Process the data without abstractions. It is faster because usually lesser number of objects are created, and lesser number of operations are performed.
What will this pure function return?
function foo(ary) {
return ary.push(1);
}
This is not a pure function because it is mutating the array it receives in the param.
How can you use a function value within a HOF?
- you can call it to make a computation
- you can pass it as an argument to another function
How can you leverage closure when working with HOFs?
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.
What does it mean for a function to be pure?
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.
What is the name of the function which allows you to select only certain elements from an array?
Array.filter
What is a Han unification?
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 is the ability to use different HOF for processing data called?
Composability