<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 &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; 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&nbsp;_OptionalProtocol {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp; &nbsp;&nbsp;associatedtype&nbsp;_Wrapped<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp; &nbsp;&nbsp;var&nbsp;_optional:&nbsp;_Wrapped? {&nbsp;get&nbsp;}<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&nbsp;Optional:&nbsp;_OptionalProtocol&nbsp;{<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp; &nbsp;&nbsp;var&nbsp;_optional:&nbsp;Wrapped? {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;self<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp; &nbsp;&nbsp;}<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&nbsp;Sequence&nbsp;where&nbsp;Element: _OptionalProtocol {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp; &nbsp;&nbsp;func&nbsp;compacted() -&gt; [Element._Wrapped] {<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;return&nbsp;flatMap&nbsp;{ $0._optional&nbsp;}<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>&nbsp; &nbsp;&nbsp;}<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&nbsp;a = [1,&nbsp;nil,&nbsp;2]<br class=""><span class="Apple-tab-span" style="white-space: pre;">        </span>a.compacted()&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;// [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="">--&nbsp;</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>