<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=""><span class="Apple-tab-span" style="white-space:pre">        </span>Having watched this discussion without much opinion before, I’ve found the argument about the meaning of map in a functional context to be somewhat convincing. While Swift isn’t a pure functional language, I think having the concept of a `map` generally mean a 1:1 mapping between input and output to be a good bit of consistency when thinking about it abstractly. Therefore I think a new verb is necessary, so compact is my choice, similar to flatten. While it could be considered a very specific version of filter, I think that it can stand on its own, as it also unwraps. So compact() would compact any array of optionals and compactMap() would map and then compact. While that sort of map isn’t 1:1, it’s in the line of normal flatMap, where the transform may change the number of elements in the result beyond a simple flattening of the array.<div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>As for popularity, personally I compact arrays of optionals far more than I flatten arrays of arrays. It’s especially useful in dealing with Objective-C types.&nbsp;</div><div class=""><span class="Apple-tab-span" style="white-space:pre">        </span>Additionally, I think the logic I’ve just described justifies a flatten/flattened function to parallel flatMap. I know it would simplify my code, as I’m usually only flattening a single level of array without a transform.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Jon Shier<br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Nov 15, 2017, at 7: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=""><blockquote type="cite" class=""><div class=""><br class="Apple-interchange-newline">On Nov 15, 2017, at 2:31 PM, BJ Homer via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Nov 15, 2017, at 3:05 PM, Tino Heth via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class=""><br class=""></div>Odd… exactly that is the reason why I think filterMap is the worst choice:</div></div></blockquote><blockquote type="cite" class=""><br class=""></blockquote><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class="">Both are established terms of art, but one has a meaning that doesn’t fit to the operation.</div><div class="">Applying filter can remove elements, but it can never change types (I feel kind of silly to repeat this over and over, but so far, nobody took the time to falsify this).</div></div></div></blockquote></div><br class=""><div class="">The concern about<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">filter</font><span class="Apple-converted-space">&nbsp;</span>changing types is only relevant if you think of the filter applying to the<span class="Apple-converted-space">&nbsp;</span><i class="">result</i>&nbsp;of the map, instead of being a part of the<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">filterMap</font><span class="Apple-converted-space">&nbsp;</span>operation itself (an operation that is distinct from<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">map</font>).</div><div class=""><br class=""></div><div class="">Let’s imagine that we had this instead:</div><div class=""><br class=""></div><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;"><span class="" style="color: rgb(186, 45, 162);">enum</span><span class="Apple-converted-space">&nbsp;</span>SelectiveMapResult&lt;T&gt; {</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">case</span><span class="Apple-converted-space">&nbsp;</span>use(<span class="" style="color: rgb(79, 129, 135);">T</span>)</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">case</span><span class="Apple-converted-space">&nbsp;</span>ignore</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">}</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><span class="" style="font-size: 11px;"><br class=""></span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">extension<span class=""><span class="Apple-converted-space">&nbsp;</span></span><span class="" style="color: rgb(112, 61, 170);">Sequence</span><span class=""><span class="Apple-converted-space">&nbsp;</span>{</span></span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">&nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">func</span><span class="Apple-converted-space">&nbsp;</span>selectiveMap&lt;T&gt;(<span class="" style="color: rgb(186, 45, 162);">_</span><span class="Apple-converted-space">&nbsp;</span>selectiveTransform: (<span class="" style="color: rgb(112, 61, 170);">Element</span>)-&gt;<span class="" style="color: rgb(79, 129, 135);">SelectiveMapResult</span>&lt;<span class="" style="color: rgb(79, 129, 135);">T</span>&gt;) -&gt; [<span class="" style="color: rgb(79, 129, 135);">T</span>]</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">}</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;"><span class="" style="font-size: 11px;"><br class=""></span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;"><span class="" style="color: rgb(186, 45, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>actualNumbers =</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">&nbsp; &nbsp; [<span class="" style="color: rgb(209, 47, 27);">"1"</span>,<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(209, 47, 27);">"2"</span>,<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(209, 47, 27);">"apple"</span>,<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(209, 47, 27);">"banana"</span>,<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(209, 47, 27);">"5"</span>].<span class="" style="color: rgb(49, 89, 93);">selectiveMap</span>({ (x)-&gt;<span class="" style="color: rgb(79, 129, 135);">SelectiveMapResult</span>&lt;<span class="" style="color: rgb(112, 61, 170);">Int</span>&gt;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">in</span></span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">if</span><span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">let</span><span class="Apple-converted-space">&nbsp;</span>value =<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(112, 61, 170);">Int</span>(x) {<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">return</span><span class="Apple-converted-space">&nbsp;</span>.<span class="" style="color: rgb(49, 89, 93);">use</span>(value) }</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">&nbsp; &nbsp; &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">else</span><span class="Apple-converted-space">&nbsp;</span>{<span class="Apple-converted-space">&nbsp;</span><span class="" style="color: rgb(186, 45, 162);">return</span><span class="Apple-converted-space">&nbsp;</span>.<span class="" style="color: rgb(49, 89, 93);">ignore</span><span class="Apple-converted-space">&nbsp;</span>}</span></div></div><div class=""><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">&nbsp; &nbsp; })</span></div></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;"><br class=""></span></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><span class="" style="font-size: 11px;">actualNumbers == [1, 2, 5]</span></div></blockquote><div class="" style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);"><br class=""></div><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);">The “selective” part of this operation doesn’t feel like it’s changing the type of the result, because<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">SelectiveMapResult</font><span class="Apple-converted-space">&nbsp;</span>is easily understood to not be part of the mapping transformation; it just exists to tell us whether we should<span class="Apple-converted-space">&nbsp;</span><i class="">use the result</i><span class="Apple-converted-space">&nbsp;</span>of that particular transformation. Likewise, I don’t feel like the optional in<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">filterMap</font><span class="Apple-converted-space">&nbsp;</span>is part of the mapping operation; it’s just serving the same role as<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">SelectiveMapResult</font>. (It should be obvious that<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">SelectiveMapResult</font><span class="Apple-converted-space">&nbsp;</span>is just<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">Optional</font><span class="Apple-converted-space">&nbsp;</span>with another name here.)</div></div></div></blockquote><div class=""><br class=""></div><div class="">"selectiveMap" feels better in part due to grammar. "map" is obviously the verb and "selective" is obviously a modification of "map". "selectiveMap" is therefore performing some sort of special map operation.&nbsp;</div><div class=""><br class=""></div><div class="">"filterMap" feels bad for the same reason that "selectMap" would feel worse than "selectiveMap". "filter" and "map" are both verbs in this context. Grammatically the analogue to "selectiveMap" would be "filteredMap" or "filteringMap".&nbsp;</div><div class=""><br class=""></div><div class="">But even then "filteredMap" or "filteringMap" is insufficient to describe the operation. You additionally need to know that the "filter" here is not ordinary "filter", but instead the special case "filter { $0 != nil }".</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote type="cite" class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;"><div class="" style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);">The name<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">filterMap</font><span class="Apple-converted-space">&nbsp;</span>focuses on removing the ignored values, as does<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">compactMap</font>. The name<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">selectiveMap</font><span class="Apple-converted-space">&nbsp;</span>focuses on retaining the non-ignored values. I’m not sure whether focusing on the positive or negative aspects is clearer here. I don’t particularly like the name<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">compactMap</font>, simply because I don’t have a lot of experience with languages that use “compact” to mean “drop the nil values”, and without that experience it doesn’t seem intuitive. I think filterMap is better. But if we introduced<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">Sequence.compact()</font><span class="Apple-converted-space">&nbsp;</span>alongside<span class="Apple-converted-space">&nbsp;</span><font face="Menlo" class="">.compactMap()</font>, I’d probably get used to it.</div></div></div></blockquote><br class=""></div><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="">Swift doesn't use "filter" to mean "drop the nil values" either.&nbsp;</div><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=""><br class=""></div><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=""><br class=""></div><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 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=""><br class=""></div><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="">"mapSome" is great if you know about Optional.Some but terrible if you don't. ("Okay, it maps some elements, but which ones?")&nbsp;</div><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=""><br class=""></div><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="">"mapNonNil" is obvious and ugly and perhaps its obviousness makes it a winner.</div><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=""><br class=""></div><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=""><br class=""></div><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="">--&nbsp;</div><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="">Greg Parker &nbsp; &nbsp;<span class="Apple-converted-space">&nbsp;</span><a href="mailto:gparker@apple.com" class="">gparker@apple.com</a>&nbsp; &nbsp; &nbsp;Runtime Wrangler</div><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=""><br class=""></div><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=""><br class=""></div><span 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; float: none; display: inline !important;" class="">_______________________________________________</span><br 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=""><span 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; float: none; display: inline !important;" class="">swift-evolution mailing list</span><br 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=""><a href="mailto:swift-evolution@swift.org" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">swift-evolution@swift.org</a><br 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=""><a href="https://lists.swift.org/mailman/listinfo/swift-evolution" style="font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a></div></blockquote></div><br class=""></div></body></html>