Recall & Review
beginner
What does the
map function do in Swift?The
map function takes a collection and applies a transformation to each item, returning a new collection with the transformed items.Click to reveal answer
beginner
Explain the purpose of the
filter function.The
filter function selects elements from a collection that meet a condition, returning a new collection with only those elements.Click to reveal answer
intermediate
What is the role of
reduce in Swift collections?reduce combines all elements of a collection into a single value by repeatedly applying a combining function, starting from an initial value.Click to reveal answer
beginner
Given an array
[1, 2, 3], what is the result of map { $0 * 2 }?The result is
[2, 4, 6] because each number is multiplied by 2.Click to reveal answer
beginner
How would you use
filter to get only even numbers from [1, 2, 3, 4]?Use
filter { $0 % 2 == 0 } to get [2, 4], selecting only numbers divisible by 2.Click to reveal answer
What does the
map function return?✗ Incorrect
map transforms each element and returns a new collection with those transformed elements.Which function would you use to select elements based on a condition?
✗ Incorrect
filter selects elements that satisfy a condition.What is the initial value used for in
reduce?✗ Incorrect
The initial value is the starting point for combining elements in
reduce.What will
[1, 2, 3].filter { $0 > 1 } return?✗ Incorrect
It returns elements greater than 1, which are 2 and 3.
Which pattern combines all elements into one value?
✗ Incorrect
reduce combines all elements into a single value.Describe how you would use map, filter, and reduce to process a list of numbers.
Think about changing, selecting, and summarizing data.
You got /3 concepts.
Explain the difference between map and filter with examples.
One changes all items, the other picks some items.
You got /4 concepts.