Beta 6 Lazy #swiftlang

Until Beta 6, lazy was defined as a standalone function. (I discussed it in this old post.)

public func lazy<Base : SequenceType>(s: Base) -> LazySequence<Base>

Beta 6 introduces a lazy property on sequences.

extension SequenceType {
    public var lazy: LazySequence<Self> { get }

This property is implemented in CollectionType, LazyCollectionType, and LazySequenceType extensions. You use it to delay evaluation on things like map and filter that are normally eager.

So, starting with Beta 6, instead of calling the function like this:

let words = "lorem ipsum elit"
    .characters.split(isSeparator: {$0 == " "}) 
let counts = lazy(words).map(count)

You use the in-line property like this:

let counts ={$0.characters.count})

Thanks, Lily Ballard

Comments are closed.