Complete the code to create a lazy collection from an array.
let numbers = [1, 2, 3, 4, 5] let lazyNumbers = numbers.[1]
map or filter directly creates eager collections.Using lazy creates a lazy collection that delays computation until needed.
Complete the code to lazily map numbers to their squares.
let numbers = [1, 2, 3, 4, 5] let squares = numbers.lazy.[1] { $0 * $0 }
filter instead of map.The map function transforms each element, and combined with lazy, it delays the computation.
Fix the error in the code to lazily filter even numbers.
let numbers = [1, 2, 3, 4, 5] let evens = numbers.[1].filter { $0 % 2 == 0 }
filter directly without lazy causes eager evaluation.You must call lazy before filter to make the filtering lazy.
Fill both blanks to lazily map numbers to strings.
let numbers = [1, 2, 3, 4, 5] let strings = numbers.[1].[2] { "Number: \($0)" }
map and lazy or missing lazy.First call lazy to delay operations, then map to transform elements.
Fill all three blanks to create a lazy collection that filters numbers greater than 3, maps them to strings, and then collects the results.
let numbers = [1, 2, 3, 4, 5] let result = Array(numbers.[1].[2] { $0 > 3 }.[3] { "Value: \($0)" })
reduce instead of map or missing lazy.Use lazy to delay, then filter to select, and map to transform.
