Daily iOS
Swift

Implementing compactMap

Extend Array with a myCompactMap that behaves like the standard library's compactMap. How would you write the generic signature?

How would an interviewer hear this answer? You'll see whether they'd move on, dig in, or say no, and which sentences would trip you up. Your score doesn't change.
See the short answer

In an Array extension, declare a single generic parameter T, take a closure (Element) -> T? that maps an element to an optional, and return [T]. The input type already comes from Array's Element, so only the result type needs to be generic; the body calls the closure per element, skips nil, and appends only the values.

Share a question