<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=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 15, 2017, at 2:31 PM, BJ Homer via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="">On Nov 15, 2017, at 3:05 PM, Tino Heth via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</div><div class=""><meta http-equiv="Content-Type" content="text/html; charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><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 style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><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 <font face="Menlo" class="">filter</font> changing types is only relevant if you think of the filter applying to the <i class="">result</i> of the map, instead of being a part of the <font face="Menlo" class="">filterMap</font> operation itself (an operation that is distinct from <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 style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""><span style="color: rgb(186, 45, 162);" class="">enum</span> SelectiveMapResult<T> {</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""> <span style="color: #ba2da2" class="">case</span> use(<span style="color: #4f8187" class="">T</span>)</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""> <span style="color: #ba2da2" class="">case</span> ignore</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class="">}</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><span style="font-size: 11px;" class=""><br class=""></span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; color: rgb(186, 45, 162); background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class="">extension<span style="" class=""> </span><span style="color: #703daa" class="">Sequence</span><span style="" class=""> {</span></span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""> <span style="color: #ba2da2" class="">func</span> selectiveMap<T>(<span style="color: #ba2da2" class="">_</span> selectiveTransform: (<span style="color: #703daa" class="">Element</span>)-><span style="color: #4f8187" class="">SelectiveMapResult</span><<span style="color: #4f8187" class="">T</span>>) -> [<span style="color: #4f8187" class="">T</span>]</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class="">}</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255); min-height: 14px;" class=""><span style="font-size: 11px;" class=""><br class=""></span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""><span style="color: #ba2da2" class="">let</span> actualNumbers =</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""> [<span style="color: #d12f1b" class="">"1"</span>, <span style="color: #d12f1b" class="">"2"</span>, <span style="color: #d12f1b" class="">"apple"</span>, <span style="color: #d12f1b" class="">"banana"</span>, <span style="color: #d12f1b" class="">"5"</span>].<span style="color: #31595d" class="">selectiveMap</span>({ (x)-><span style="color: #4f8187" class="">SelectiveMapResult</span><<span style="color: #703daa" class="">Int</span>> <span style="color: #ba2da2" class="">in</span></span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""> <span style="color: #ba2da2" class="">if</span> <span style="color: #ba2da2" class="">let</span> value = <span style="color: #703daa" class="">Int</span>(x) { <span style="color: #ba2da2" class="">return</span> .<span style="color: #31595d" class="">use</span>(value) }</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""> <span style="color: #ba2da2" class="">else</span> { <span style="color: #ba2da2" class="">return</span> .<span style="color: #31595d" class="">ignore</span> }</span></div></div><div class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""> })</span></div></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class=""><br class=""></span></div><div style="margin: 0px; font-stretch: normal; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><span style="font-size: 11px;" class="">actualNumbers == [1, 2, 5]</span></div></blockquote><div style="margin: 0px; font-stretch: normal; font-size: 11px; line-height: normal; font-family: Menlo; background-color: rgb(255, 255, 255);" class=""><br class=""></div><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class="">The “selective” part of this operation doesn’t feel like it’s changing the type of the result, because <font face="Menlo" class="">SelectiveMapResult</font> is easily understood to not be part of the mapping transformation; it just exists to tell us whether we should <i class="">use the result</i> of that particular transformation. Likewise, I don’t feel like the optional in <font face="Menlo" class="">filterMap</font> is part of the mapping operation; it’s just serving the same role as <font face="Menlo" class="">SelectiveMapResult</font>. (It should be obvious that <font face="Menlo" class="">SelectiveMapResult</font> is just <font face="Menlo" class="">Optional</font> with another name here.)</div></div></div></blockquote><div><br class=""></div><div>"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. </div><div><br class=""></div><div>"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". </div><div><br class=""></div><div>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><br class=""></div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div style="margin: 0px; font-stretch: normal; line-height: normal; background-color: rgb(255, 255, 255);" class="">The name <font face="Menlo" class="">filterMap</font> focuses on removing the ignored values, as does <font face="Menlo" class="">compactMap</font>. The name <font face="Menlo" class="">selectiveMap</font> 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 <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 <font face="Menlo" class="">Sequence.compact()</font> alongside <font face="Menlo" class="">.compactMap()</font>, I’d probably get used to it.</div></div></div></blockquote><br class=""></div><div>Swift doesn't use "filter" to mean "drop the nil values" either. </div><div><br class=""></div><div><br class=""></div><div>"compactMap" is okay if "compact" is added. Is "compact" a common enough operation in practice to pull its own weight?</div><div><br class=""></div><div>"mapSome" is great if you know about Optional.Some but terrible if you don't. ("Okay, it maps some elements, but which ones?") </div><div><br class=""></div><div>"mapNonNil" is obvious and ugly and perhaps its obviousness makes it a winner.</div><div><br class=""></div><div><br class=""></div><div>-- </div><div>Greg Parker <a href="mailto:gparker@apple.com" class="">gparker@apple.com</a> Runtime Wrangler</div><div><br class=""></div><div><br class=""></div></body></html>