Implementing compactMap
Extend Array with a myCompactMap that behaves like the standard library's compactMap. How would you write the generic signature?
Extend Array with a myCompactMap that behaves like the standard library's compactMap. How would you write the generic signature?
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.
SwiftShort answerBasics
Extend Optional so that any optional can be asked for its nil status, like…
#extension #optional
SwiftShort answerAdvanced
Suppose there were no question-mark optional syntax and no standard Optional. If you had…
#generics #optional
SwiftMultiple choiceIntro
Which of these unwrap an optional safely? Pick all that apply.
#optional
SwiftMultiple choiceExpert
What does this code print? …
#extension