Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a lazy collection from an array.
Swift
let numbers = [1, 2, 3, 4, 5] let lazyNumbers = numbers.[1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
map or filter directly creates eager collections.✗ Incorrect
Using lazy creates a lazy collection that delays computation until needed.
2fill in blank
mediumComplete the code to lazily map numbers to their squares.
Swift
let numbers = [1, 2, 3, 4, 5] let squares = numbers.lazy.[1] { $0 * $0 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
filter instead of map.✗ Incorrect
The map function transforms each element, and combined with lazy, it delays the computation.
3fill in blank
hardFix the error in the code to lazily filter even numbers.
Swift
let numbers = [1, 2, 3, 4, 5] let evens = numbers.[1].filter { $0 % 2 == 0 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling
filter directly without lazy causes eager evaluation.✗ Incorrect
You must call lazy before filter to make the filtering lazy.
4fill in blank
hardFill both blanks to lazily map numbers to strings.
Swift
let numbers = [1, 2, 3, 4, 5] let strings = numbers.[1].[2] { "Number: \($0)" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping
map and lazy or missing lazy.✗ Incorrect
First call lazy to delay operations, then map to transform elements.
5fill in blank
hardFill all three blanks to create a lazy collection that filters numbers greater than 3, maps them to strings, and then collects the results.
Swift
let numbers = [1, 2, 3, 4, 5] let result = Array(numbers.[1].[2] { $0 > 3 }.[3] { "Value: \($0)" })
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using
reduce instead of map or missing lazy.✗ Incorrect
Use lazy to delay, then filter to select, and map to transform.