0
0
Swiftprogramming~5 mins

Map, filter, reduce patterns in Swift - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe original collection unchanged
BA single combined value
CA new collection with transformed elements
DOnly elements that meet a condition
Which function would you use to select elements based on a condition?
Afilter
Bmap
Creduce
Dsort
What is the initial value used for in reduce?
ATo start combining elements
BTo filter elements
CTo transform elements
DTo sort elements
What will [1, 2, 3].filter { $0 > 1 } return?
A[]
B[1]
C[1, 2, 3]
D[2, 3]
Which pattern combines all elements into one value?
Amap
Breduce
Cfilter
DforEach
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.