<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 &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; 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 &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; 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 &lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt; 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&lt;T&gt; {</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&lt;T&gt;(<span style="color:#ba2da2">_</span> selectiveTransform: (<span style="color:#703daa">Element</span>)-&gt;<span style="color:#4f8187">SelectiveMapResult</span>&lt;<span style="color:#4f8187">T</span>&gt;) -&gt; [<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">&quot;1&quot;</span>, <span style="color:#d12f1b">&quot;2&quot;</span>, <span style="color:#d12f1b">&quot;apple&quot;</span>, <span style="color:#d12f1b">&quot;banana&quot;</span>, <span style="color:#d12f1b">&quot;5&quot;</span>].<span style="color:#31595d">selectiveMap</span>({ (x)-&gt;<span style="color:#4f8187">SelectiveMapResult</span>&lt;<span style="color:#703daa">Int</span>&gt; <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>&quot;selectiveMap&quot; feels better in part due to grammar. &quot;map&quot; is obviously the verb and &quot;selective&quot; is obviously a modification of &quot;map&quot;. &quot;selectiveMap&quot; is therefore performing some sort of special map operation. </div><div><br></div><div>&quot;filterMap&quot; feels bad for the same reason that &quot;selectMap&quot; would feel worse than &quot;selectiveMap&quot;. &quot;filter&quot; and &quot;map&quot; are both verbs in this context. Grammatically the analogue to &quot;selectiveMap&quot; would be &quot;filteredMap&quot; or &quot;filteringMap&quot;. </div><div><br></div><div>But even then &quot;filteredMap&quot; or &quot;filteringMap&quot; is insufficient to describe the operation. You additionally need to know that the &quot;filter&quot; here is not ordinary &quot;filter&quot;, but instead the special case &quot;filter { $0 != nil }&quot;.</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&#39;t use &quot;filter&quot; to mean &quot;drop the nil values&quot; either. </div><div><br></div><div><br></div><div>&quot;compactMap&quot; is okay if &quot;compact&quot; is added. Is &quot;compact&quot; a common enough operation in practice to pull its own weight?</div><div><br></div><div>&quot;mapSome&quot; is great if you know about Optional.Some but terrible if you don&#39;t. (&quot;Okay, it maps some elements, but which ones?&quot;) </div><div><br></div><div>&quot;mapNonNil&quot; 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>