<div><div><div dir="auto">I vote for “mapNonNil”, “mapDroppingNil” or “unwrappingMap”(or some variations using unwrap).</div><div dir="auto"><br></div><div dir="auto">It’s not pretty, but does the job!</div></div></div><div dir="auto"><br></div><div dir="auto"><br></div><div><div><br><div class="gmail_quote"><div>Em qua, 15 de nov de 2017 às 22:36, Greg Parker via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> escreveu:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word;line-break:after-white-space"><div><blockquote type="cite"><div>On Nov 15, 2017, at 2:31 PM, BJ Homer via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><div><div style="word-wrap:break-word;line-break:after-white-space"><br><div><blockquote type="cite"><div>On Nov 15, 2017, at 3:05 PM, Tino Heth via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> wrote:</div><div><div style="word-wrap:break-word;line-break:after-white-space"><div><br></div>Odd… exactly that is the reason why I think filterMap is the worst choice:</div></div></blockquote><blockquote type="cite"><br></blockquote><blockquote type="cite"><div><div style="word-wrap:break-word;line-break:after-white-space"><div>Both are established terms of art, but one has a meaning that doesn’t fit to the operation.</div><div>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><div>The concern about <font face="Menlo">filter</font> changing types is only relevant if you think of the filter applying to the <i>result</i> of the map, instead of being a part of the <font face="Menlo">filterMap</font> operation itself (an operation that is distinct from <font face="Menlo">map</font>).</div><div><br></div><div>Let’s imagine that we had this instead:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"><span style="color:rgb(186,45,162)">enum</span> SelectiveMapResult<T> {</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"> <span style="color:#ba2da2">case</span> use(<span style="color:#4f8187">T</span>)</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"> <span style="color:#ba2da2">case</span> ignore</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px">}</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><span style="font-size:11px"><br></span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;color:rgb(186,45,162);background-color:rgb(255,255,255)"><span style="font-size:11px">extension<span> </span><span style="color:#703daa">Sequence</span><span> {</span></span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"> <span style="color:#ba2da2">func</span> selectiveMap<T>(<span style="color:#ba2da2">_</span> selectiveTransform: (<span style="color:#703daa">Element</span>)-><span style="color:#4f8187">SelectiveMapResult</span><<span style="color:#4f8187">T</span>>) -> [<span style="color:#4f8187">T</span>]</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px">}</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255);min-height:14px"><span style="font-size:11px"><br></span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"><span style="color:#ba2da2">let</span> actualNumbers =</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"> [<span style="color:#d12f1b">"1"</span>, <span style="color:#d12f1b">"2"</span>, <span style="color:#d12f1b">"apple"</span>, <span style="color:#d12f1b">"banana"</span>, <span style="color:#d12f1b">"5"</span>].<span style="color:#31595d">selectiveMap</span>({ (x)-><span style="color:#4f8187">SelectiveMapResult</span><<span style="color:#703daa">Int</span>> <span style="color:#ba2da2">in</span></span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"> <span style="color:#ba2da2">if</span> <span style="color:#ba2da2">let</span> value = <span style="color:#703daa">Int</span>(x) { <span style="color:#ba2da2">return</span> .<span style="color:#31595d">use</span>(value) }</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"> <span style="color:#ba2da2">else</span> { <span style="color:#ba2da2">return</span> .<span style="color:#31595d">ignore</span> }</span></div></div><div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"> })</span></div></div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px"><br></span></div><div style="margin:0px;font-stretch:normal;line-height:normal;font-family:Menlo;background-color:rgb(255,255,255)"><span style="font-size:11px">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)"><br></div><div 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 <font face="Menlo">SelectiveMapResult</font> is easily understood to not be part of the mapping transformation; it just exists to tell us whether we should <i>use the result</i> of that particular transformation. Likewise, I don’t feel like the optional in <font face="Menlo">filterMap</font> is part of the mapping operation; it’s just serving the same role as <font face="Menlo">SelectiveMapResult</font>. (It should be obvious that <font face="Menlo">SelectiveMapResult</font> is just <font face="Menlo">Optional</font> with another name here.)</div></div></div></blockquote><div><br></div></div></div><div style="word-wrap:break-word;line-break:after-white-space"><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></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></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></div><div style="word-wrap:break-word;line-break:after-white-space"><div><div><br></div><div><br></div><blockquote type="cite"><div><div style="word-wrap:break-word;line-break:after-white-space"><div style="margin:0px;font-stretch:normal;line-height:normal;background-color:rgb(255,255,255)">The name <font face="Menlo">filterMap</font> focuses on removing the ignored values, as does <font face="Menlo">compactMap</font>. The name <font face="Menlo">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">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">Sequence.compact()</font> alongside <font face="Menlo">.compactMap()</font>, I’d probably get used to it.</div></div></div></blockquote><br></div></div><div style="word-wrap:break-word;line-break:after-white-space"><div></div><div>Swift doesn't use "filter" to mean "drop the nil values" either. </div><div><br></div><div><br></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></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></div><div>"mapNonNil" is obvious and ugly and perhaps its obviousness makes it a winner.</div></div><div style="word-wrap:break-word;line-break:after-white-space"><div><br></div><div><br></div><div>-- </div><div>Greg Parker <a href="mailto:gparker@apple.com" target="_blank">gparker@apple.com</a> Runtime Wrangler</div><div><br></div><div><br></div></div>_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
</blockquote></div></div></div>