skip to content

Sequence Pipelines and Laziness

Sequence and Collection give every container map, filter, reduce, compactMap and lazy views. Interviewers ask for one-liners, the cost of chaining and when lazy avoids building arrays.

on this pageshow

explore

questions

6

In Swift, what is the difference between map, compactMap and flatMap when you transform a sequence of values?

level: juniorimportance: must knowfreq 75%

answer

  1. count of outputs per input
  2. optional results dropped
  3. nested results concatenated
  4. the Swift 4.1 rename

basics

~10 s

Swift's map returns exactly one result per element; compactMap keeps only the non-nil results of a closure that returns an optional; flatMap concatenates the sequences its closure returns into one flat array.

solid answer

~40 s

All three are `Sequence` methods that run the closure eagerly and return an `Array`. `map(_:)` is one-to-one: parsing four log lines with a failable `Event(line:)` gives an `[Event?]` of four elements, `nil`s included. `compactMap(_:)` takes a closure returning an optional and drops the `nil`s, so the same call gives an `[Event]` holding only the lines that parsed. `flatMap(_:)` takes a closure returning a sequence and appends every result, turning per-session `[[Event]]` into one `[Event]`; the standard library documents it as equivalent to `Array(s.map(transform).joined())`. Calling `flatMap` with an optional-returning closure is the spelling from before Swift 4.1: it still compiles, with a deprecation warning that points to `compactMap(_:)`.

code

swift · 22 lines
swift
struct Event {
    let name: String
    let userID: Int

    init?(line: String) {
        let parts = line.split(separator: ",")
        guard parts.count == 2, let id = Int(parts[1]) else { return nil }
        name = String(parts[0])
        userID = id
    }
}

let lines = ["open,1", "garbage", "tap,2", "close,x"]

let maybe = lines.map { Event(line: $0) }          // [Event?], 4 elements
let events = lines.compactMap { Event(line: $0) }  // [Event], 2 elements

let sessions = [["open,1", "tap,1"], ["open,2"]]
let nested = sessions.map { $0.compactMap(Event.init(line:)) }   // [[Event]]
let flat = sessions.flatMap { $0.compactMap(Event.init(line:)) } // [Event]

print(maybe.count, events.count, nested.count, flat.count)

go deeper

for a junior

Recall the three closure shapes: map returns a value, compactMap returns an optional, flatMap returns a sequence. State the result type each produces for a failable parse.

for a middle

Explain why compactMap is one pass and one allocation, why map on a Set returns an array, and why the optional flatMap overload is deprecated since Swift 4.1.

for a senior

Spot the String-flattening surprise and deprecated flatMap calls in review, and know when an eager chain of transforms should become a lazy one or a single loop.

for a principal

Set a readable team convention: compactMap for optional results, flatMap only for real flattening, and fix deprecation warnings rather than leaving them to accumulate across modules.

## Three shapes of transform Every Swift type that conforms to `Sequence` (arrays, sets, dictionaries, ranges, strings, your own sequences) inherits a family of **higher-order methods**: methods that take a closure and apply it to each element. Three of them transform elements, and they differ in what the closure returns and what happens to it. | Method | Closure returns | Result | Output count | |---|---|---|---| | `map(_:)` | `T` | `[T]` | exactly one per input | | `compactMap(_:)` | `T?` | `[T]` | zero or one per input | | `flatMap(_:)` | some `Sequence` of `T` | `[T]` | any number per input | All three are **eager**: they run the closure on every element immediately and build a new `Array`. That holds even when you call them on a `Set` or a `Dictionary`, so `Set.map` returns an array, not a set. ## map: one in, one out `map(_:)` never changes the number of elements. If the closure returns an optional, the optional ends up in the array. Parsing a day's raw event log with a failable initializer `Event(line:)` shows it: - `lines.map { Event(line: $0) }` over four lines gives `[Event?]` with four elements, two of them `nil`. - The type tells you the result still has to be unwrapped somewhere downstream. ## compactMap: transform and drop nil in one pass `compactMap(_:)` was introduced in **Swift 4.1** for exactly this case. Its closure returns `T?`; the method keeps the non-`nil` results and discards the rest, so `lines.compactMap { Event(line: $0) }` gives an `[Event]` with only the lines that parsed. Points worth stating precisely: - It removes `nil` and nothing else: an empty string, a zero or an empty array survive. - It does the work in **one pass** and builds one array, where `map` followed by `filter` and a force unwrap would build two. - Its documented complexity is O(*n*) in the length of the input. ## flatMap: one in, many out `flatMap(_:)` takes a closure that returns a **sequence** and appends every element of each result to one array. The standard library's doc comment defines it as equivalent to `Array(s.map(transform).joined())`. In the analytics scenario, sessions stored as an array of arrays of raw lines become a single list of events with `sessions.flatMap { $0.compactMap(Event.init(line:)) }`. Its documented complexity is O(*m* + *n*), where *n* is the input length and *m* the length of the result. ## The deprecated flatMap and a String trap Before Swift 4.1, `flatMap` also had an overload for optional-returning closures, which is why older code drops `nil`s with `flatMap`. That overload is still in the standard library, marked deprecated since 4.1 with the message `Please use compactMap(_:) for the case where closure returns an optional value`. The Swift 6.4 compiler accepts it with a warning, in either language mode. A second trap comes from `String` being a `Sequence` of `Character`: 1. `["hi", "yo"].flatMap { $0 }` picks the sequence overload, because each `String` is a sequence. 2. The result is `[Character]`: `["h", "i", "y", "o"]`, not the two strings. 3. If the intent was to keep the strings, `map` (or no transform at all) is what you meant. ## Cost and laziness Because all three are eager, a chain such as `lines.compactMap(parse).map(enrich)` allocates one array per stage. That is usually fine for a few thousand log lines. When the chain is long, the input is huge, or you only need the first match, calling `.lazy` first turns these methods into lazy views that compute on demand. For the transforms themselves the rule of thumb is simple: - Count stays the same: `map`. - Some inputs produce nothing: `compactMap`. - Some inputs produce several outputs: `flatMap`. ## What the implementations reveal Reading the standard library's source sharpens two details interviewers like: - `map(_:)` asks the sequence for its `underestimatedCount` (the exact `count` for a collection) and reserves that capacity before the loop, so mapping an array allocates its result once. - `compactMap(_:)` and `filter(_:)` cannot know how many elements survive, so they start from an empty array and grow it as they append. - The closure may throw: `map(_:)` propagates the closure's error with typed throws, and `compactMap(_:)` and `flatMap(_:)` are `rethrows`, so a throwing parser needs `try` at the call site and nowhere else. `Optional` has its own `map` and `flatMap`, which unwrap a single optional rather than transform a collection; the Swift lesson on optionals covers them.

  • What does `["hi", "yo"].flatMap { $0 }` return in Swift, and why?
    It returns `[Character]`, `["h", "i", "y", "o"]`. `String` conforms to `Sequence` with `Character` elements, so the closure's result is a sequence and the flattening overload of `flatMap` is chosen. Use `map` or no transform if you want to keep whole strings.
  • Why was the optional-returning `flatMap` renamed to `compactMap`?
    One name covered two different operations: flattening nested sequences and discarding `nil`s. That made code hard to read and let the type checker pick a surprising overload. Swift 4.1 (SE-0187) added `compactMap(_:)` and deprecated the optional overload of `flatMap`; it still compiles today with a warning pointing at `compactMap(_:)`.
  • Is `compactMap(f)` the same as `map(f).filter { $0 != nil }.map { $0! }`?
    The result is the same, but the eager chain builds three arrays and force-unwraps; `compactMap` makes one pass and one array. On a lazy sequence the standard library actually implements `compactMap` as exactly that map-filter-map chain of lazy views, which costs nothing until elements are read.

  • compactMap also removes empty strings, zeros and empty arrays
  • map can return fewer elements than the input when the closure returns nil
  • flatMap is still the preferred way to drop nil results from an array
  • Calling map on a Set returns a Set
  • map and compactMap are lazy and only run when the result is read
open as a page

In a Swift analytics pipeline, why is reduce(into:_:) preferred over reduce(_:_:) when counting a day's events into a Dictionary?

level: middleimportance: should knowfreq 50%

basics

~20 s

With reduce(::) the closure must return a new Dictionary, and mutating a copy of a buffer the standard library still holds copies it on every element; reduce(into:_:) passes the Dictionary inout and mutates it in place.

open as a page

In Swift, what does conforming to Collection promise beyond Sequence, and why must an endless ID generator stay a plain Sequence?

level: middleimportance: should knowfreq 45%

basics

~20 s

Swift's Sequence only promises an iterator whose next() returns elements until nil; Collection adds nondestructive multipass access through indices, startIndex and endIndex, and every collection is finite, so an endless generator can only be a Sequence.

open as a page

In Swift, is sorted(by:) a stable sort, and what must its areInIncreasingOrder closure guarantee for the result to be correct?

level: middleimportance: should knowfreq 40%

basics

~10 s

Yes: Swift documents sort() and sorted(by:) as stable, so elements the predicate does not order keep their original relative order. The predicate must be a strict weak ordering, so less-or-equal comparisons break the guarantee.

open as a page

In Swift, why does subscripting an ArraySlice with the counter from enumerated() pick the wrong element or trap, and what should you use instead?

level: seniorimportance: should knowfreq 35%

basics

~20 s

enumerated() yields an Int counter starting at 0, not an index; an ArraySlice keeps its parent array's indices, so the counter points at the wrong element or past the bounds. Pair real indices with zip(slice.indices, slice).

open as a page

In Swift, when does adding .lazy to a map or filter chain save work, and why can its closure run more than once per element?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Swift's .lazy saves work when you read only part of a pipeline or make a single pass, because no intermediate arrays are built; the lazy view stores the closure, not results, so every read of an element runs it again.

open as a page