When lazily mapping an array of values, I receive an instance of type LazyMapSequence
as expected:
Welcome to Apple Swift version 5.7 (swiftlang-5.7.0.127.4 clang-1400.0.29.50).
Type :help for assistance.
1> let numbers = Array(1...5)
numbers: [Int] = 5 values {
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
}
2> let squares = numbers.lazy.map { $0 * $0 }
squares: LazyMapSequence<LazySequence<[Int]>.Elements, Int> = {
_base = 5 values {
[0] = 1
[1] = 2
[2] = 3
[3] = 4
[4] = 5
}
_transform =
}
However, if the map(_:)
method receives a throwing a closure instead, the mapping is not performed lazily, and I receive an array instead:
3> func square(_ x: Int) throws -> Int {
4. return x * x
5. }
6> let squares = try numbers.lazy.map(square)
squares: [Int] = 5 values {
[0] = 1
[1] = 4
[2] = 9
[3] = 16
[4] = 25
}
Why is that, and how do I lazily map an array of values using a throwing closure?