Playground to demonstrate how lazy collections work.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
LazyCollections.playground
.gitignore
LICENSE.txt
README.md

README.md

Lazy Collections

Playground to demonstrate how lazy collections work.

map, flatMap and filter on a normal array:

let imageRequests = frameIndexes
    .filter(isFrameIncluded)
    .map(pathTransform)
    .flatMap(urlTransform)
    .map(requestTransform)

Result:

  • We iterate over the original array n times.
  • We create 3 redundant intermediate arrays.

map, flatMap and filter on a lazy array:

let lazyImageRequests = frameIndexes
    .lazy
    .filter(isFrameIncluded)
    .map(pathTransform)
    .flatMap(urlTransform)
    .map(requestTransform)
    
var imageRequests: [Request] = []
lazyImageRequests.forEach { imageRequests2.append($0) }

Result:

  • We iterate only once.
  • We get each element calculated through the pipe as we access them.