As Martin R mentioned, lazy() avoids creating an intermediate array. However, if I compare the execution time of a function with arrays of different sizes, you will find that lazy() "only" 10% faster.
Interestingly, you will find that lazy() designed for arrays with less than 200 elements 2 times faster and gets more elements almost equally fast than a function without conversion (10% faster).
(Tested with Xcode 6.4 and Xcode 7 with global functions and a protocol extension in the Playground as (compiled) source files)
So lazy() more likely to be used for Sequences , where you don't know if it is finite. Then for loops, they are probably used with break or return :
for element in lazy(sequence).map{ ... } { if element == 1000 { break }
If you call a map on an infinite Sequence (e.g. 1,2,3 ...), the execution will also be infinite. With lazy() conversion and execution are βdelayed," so you can more efficiently handle "large" and infinite sequences if you exit the loop to the last element.
Qbyte
source share