map versus compactMap versus flatMap
functional transforms on sequences.
map transforms each element one-to-one; compactMap transforms then drops nils; flatMap transforms to sequences then concatenates.
What's really being asked
It checks fluency with the standard library's transform functions and awareness of the historical flatMap-versus-compactMap rename that still trips people up.
MAP map applies a transform to each element and returns an array of the same length, one output per input. The element type can change; the count cannot. Example: [1, 2, 3].map { $0 * 2 } yields [2, 4, 6].
Compactmap
compactMap applies a transform that returns an Optional, then discards every nil and unwraps the rest, so the result can be shorter. It is the idiomatic way to transform-and-filter in one pass. Example: ["1", "x", "3"].compactMap { Int($0) } yields [1, 3] because "x" maps to nil and is dropped.
Flatmap
flatMap on a sequence applies a transform that itself returns a sequence and concatenates those sequences into a single flat array, reducing one level of nesting. Example: [[1, 2], [3, 4]].flatMap { $0 } yields [1, 2, 3, 4]. Historically flatMap also had an overload that filtered Optionals; that role moved to compactMap to remove ambiguity.
The mistakes people make
Saying flatMap removes nils, which describes the deprecated overload now served by compactMap. Claiming map can change the element count. Thinking compactMap flattens nested arrays.
What usually comes next
Why was the nil-filtering flatMap deprecated? What does flatMap do on an Optional versus a sequence? How would you flatten and filter nils in one chain? What is the complexity of these operations?
A concrete example
Parsing user input lists of comma-separated numbers across several lines, you write lines.flatMap { 0.split(separator: ",") }.compactMap { Int(0) }. flatMap concatenates the per-line tokens into one sequence, then compactMap converts each to an Int and silently drops anything that is not a valid number, giving a single clean array of integers.
Interview question
Given an array of optional strings, which call both unwraps and discards the nil entries in a single pass?
- a.flatMap, because its modern overload filters nils from sequences
- b.compactMap, because it applies the transform and drops nil resultsCorrect
- c.map, because it transforms every element
- d.None of them; you must call filter first then map
Why? this is the answer
compactMap is purpose-built to transform and drop nils. flatMap's nil-filtering role was deprecated and reassigned to compactMap, and map keeps the same count.
Just read this? Test yourself on what you have been reading.
Read the original → donnywals.com
- #swift
- #collections
- #functional
- #map
- #optionals
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on swift — each one lists the topics its interview covers.
See open roles