<div dir="ltr">I&#39;d argue that this behavior is correct. By extension of <a href="https://en.wikipedia.org/wiki/De_Morgan%27s_laws" target="_blank">De Morgan&#39;s laws</a>, &quot;seq.all { pred($0) }&quot; should be equivalent to &quot;!seq.contains { !pred($0) }&quot;.<div><div><div class="gmail_extra"><br clear="all"><div><div><div dir="ltr"><div>Jacob<br></div></div></div></div>
<br><div class="gmail_quote">On Sun, May 8, 2016 at 7:16 PM, Adriano Ferreira via swift-users <span dir="ltr">&lt;<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div style="margin:0px;line-height:normal">Hi everyone!</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;line-height:normal">I’m working on the following method:</div><div style="margin:0px;line-height:normal"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(222,52,140)"><span>extension</span><span style="color:#000000"> </span><span style="color:#587ea8">SequenceType</span><span style="color:#000000"> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(192,192,192)"><span style="color:#000000">    </span><span>/// Check if `predicate` is true for all elements of `self`</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#c0c0c0">///</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(192,192,192)"><span style="color:#000000">    </span><span>/// - Parameter predicate: The predicate called on each element of `self`</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#c0c0c0">///</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(192,192,192)"><span style="color:#000000">    </span><span>/// - Returns: True iff every element in `self` satisfies `predicate`, false otherwise</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(222,52,140)"><span style="color:#000000">    </span><span>@warn_unused_result</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    </span><span style="color:#de348c">func</span><span> all(</span><span style="color:#de348c">@noescape</span><span> </span><span style="color:#de348c">where</span><span> predicate: Generator.Element </span><span style="color:#de348c">throws</span><span> -&gt; </span><span style="color:#587ea8">Bool</span><span>) </span><span style="color:#de348c">rethrows</span><span> -&gt; </span><span style="color:#587ea8">Bool</span><span> {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>        </span><span style="color:#de348c">for</span><span> element </span><span style="color:#de348c">in</span><span> </span><span style="color:#de348c">self</span><span> </span><span style="color:#de348c">where</span><span> </span><span style="color:#de348c">try</span><span> !predicate(element) {</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>            </span><span style="color:#de348c">return</span><span> </span><span style="color:#de348c">false</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>        }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><span></span><br></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;color:rgb(222,52,140)"><span style="color:#000000">        </span><span>return</span><span style="color:#000000"> </span><span>true</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo"><span>    }</span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px">}<span></span></div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;line-height:normal;min-height:13px">However, when the sequence is empty the method returns true, which is not the desired behaviour.</div><div style="margin:0px;font-size:11px;line-height:normal;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;line-height:normal;min-height:13px"><div style="margin:0px;line-height:normal"><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span style="color:#de348c">let</span><span> a = [</span><span style="color:#587ea8">Int</span><span>]()</span></div><div style="font-family:Menlo;font-size:11px;margin:0px;line-height:normal"><span style="color:#de348c">let</span><span> b = a.</span><span style="color:#587ea8">all</span><span>(</span><span style="color:#de348c">where</span><span>: { $0 &gt; 7 })</span></div><div style="margin:0px;line-height:normal"><span style="color:rgb(88,126,168);font-family:Menlo;font-size:11px">XCTAssertFalse</span><span style="color:rgb(0,0,0);font-family:Menlo;font-size:11px">(b)   </span><font color="#c0c0c0" face="Menlo"><span style="font-size:11px">// This fails, cause there’s no guard against an empty sequence</span></font></div></div><div style="font-family:Menlo;font-size:11px"><br></div><div><span style="font-family:Menlo;font-size:11px"><span style="font-family:Helvetica;font-size:12px">Does anyone know how to </span></span>guard against an empty sequence<span style="font-family:Helvetica;font-size:12px">?</span></div><div style="font-family:Menlo;font-size:11px"><span><span style="font-family:Helvetica;font-size:12px"><br></span></span></div><div><span>I’m using Xcode 7.3.1 and Swift 2.2.</span></div><div style="font-family:Menlo;font-size:11px"><span><span style="font-family:Helvetica;font-size:12px"><br></span></span></div><div style="font-family:Menlo;font-size:11px"><span><span style="font-family:Helvetica;font-size:12px">Best,</span></span></div><div style="font-family:Menlo;font-size:11px"><span><span style="font-family:Helvetica;font-size:12px"><br></span></span></div><div><span>— A</span></div></div></div><br>_______________________________________________<br>
swift-users mailing list<br>
<a href="mailto:swift-users@swift.org" target="_blank">swift-users@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-users" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-users</a><br>
<br></blockquote></div><br></div></div></div></div>