<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div><blockquote type="cite" class=""><div class="">On Nov 15, 2017, at 4:36 PM, Greg Parker via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px;" class="">"compactMap" is okay if "compact" is added. Is "compact" a common enough operation in practice to pull its own weight?</div></div></blockquote><br class=""></div><div>Lines containing code like `flatMap { $0 }`—which could be compacting optionals or flattening nested sequences—appear 89 times in the source compatibility suite. (I happen to have it on my hard drive right now.)</div><div><br class=""></div><div>My main concern about adding a compacting method is…well, am I missing a better implementation than the slightly gross one I cooked up in a playground just now?</div><div><br class=""></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>protocol _OptionalProtocol {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> associatedtype _Wrapped<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> var _optional: _Wrapped? { get }<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>extension Optional: _OptionalProtocol {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> var _optional: Wrapped? {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> return self<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> }<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>extension Sequence where Element: _OptionalProtocol {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> func compacted() -> [Element._Wrapped] {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> return flatMap { $0._optional }<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span> }<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span><br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>let a = [1, nil, 2]<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>a.compacted() // [1, 2]<br class=""><br class=""></div><br class=""><div class="">
<span class="Apple-style-span" style="border-collapse: separate; font-variant-ligatures: normal; font-variant-east-asian: normal; font-variant-position: normal; line-height: normal; border-spacing: 0px;"><div class=""><div style="font-size: 12px; " class="">-- </div><div style="font-size: 12px; " class="">Brent Royal-Gordon</div><div style="font-size: 12px; " class="">Architechies</div></div></span>
</div>
<br class=""></body></html>