Sobes.tech
Junior — Senior

What output will we get in example №20

livecode

Task condition

This example considers an array that can contain both nested arrays and nil. It is necessary to understand which values will be printed after executing three consecutive print statements. Pay attention to the differences between the map, compactMap, and flatMap methods when working with optional elements.

let matrix = [[1, 2], nil, [3, 4], [nil, 5], nil]

print(matrix.map { $0?.count })
print(matrix.compactMap { $0 }.count)
print(matrix.compactMap { $0 }.flatMap { $0 }.count)