<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
I imagine `#underestimateCount()` is going to be your best bet, though you may not always see the results you desire
<div class=""><br class="">
</div>
<div class="">some notes:</div>
<div class=""><br class="">
</div>
<div class="">1. most would argue that the results you are describing are correct: it is vacuously true that for an empty sequence _every_ element in the sequence will satisfy any predicate you give it</div>
<div class=""><br class="">
</div>
<div class="">2. do you have the option to constrain this to CollectionType instead?</div>
<div class=""><br class="">
</div>
<div class="">3. one option would be for you to grab the generator and iteratively send it `next()` on your own - that way you could check if it returns `nil` the first time that you call it (i.e. the sequence is empty) and return whatever you desire in that
 case</div>
<div class=""><br class="">
</div>
<div class="">— Shane S</div>
<div class=""><br class="">
</div>
<div class=""><br class="">
<div>
<blockquote type="cite" class="">
<div class="">On May 8, 2016, at 7:16 PM, Adriano Ferreira via swift-users &lt;<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>&gt; wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div style="margin: 0px; line-height: normal;" class="">Hi everyone!</div>
<div style="margin: 0px; line-height: normal;" class=""><br class="">
</div>
<div style="margin: 0px; line-height: normal;" class="">I’m working on the following method:</div>
<div style="margin: 0px; line-height: normal;" class=""><br class="">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(222, 52, 140);" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">extension</span><span style="font-variant-ligatures: no-common-ligatures;" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">SequenceType</span><span style="font-variant-ligatures: no-common-ligatures;" class=""> {</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">
<br class="">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">/// 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;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #c0c0c0" class="">///</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">/// - 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;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #c0c0c0" class="">///</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(192, 192, 192);" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">/// - 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;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class="">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(222, 52, 140);" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures" class="">@warn_unused_result</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; </span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">func</span><span style="font-variant-ligatures: no-common-ligatures" class=""> all(</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">@noescape</span><span style="font-variant-ligatures: no-common-ligatures" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class=""> predicate: Generator.Element
</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">throws</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -&gt;
</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures" class="">)
</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">rethrows</span><span style="font-variant-ligatures: no-common-ligatures" class=""> -&gt;
</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">Bool</span><span style="font-variant-ligatures: no-common-ligatures" class=""> {</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>
<span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">for</span><span style="font-variant-ligatures: no-common-ligatures" class=""> element
</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">in</span><span style="font-variant-ligatures: no-common-ligatures" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">self</span><span style="font-variant-ligatures: no-common-ligatures" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">try</span><span style="font-variant-ligatures: no-common-ligatures" class=""> !predicate(element) {</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span>
<span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">return</span><span style="font-variant-ligatures: no-common-ligatures" class="">
</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">false</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; &nbsp; &nbsp; }</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class=""></span><br class="">
</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; color: rgb(222, 52, 140);" class="">
<span style="font-variant-ligatures: no-common-ligatures;" class="">&nbsp; &nbsp; &nbsp; &nbsp; </span>
<span style="font-variant-ligatures: no-common-ligatures" class="">return</span><span style="font-variant-ligatures: no-common-ligatures;" class="">
</span><span style="font-variant-ligatures: no-common-ligatures" class="">true</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo;" class="">
<span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp; &nbsp; }</span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">
}<span style="font-variant-ligatures: no-common-ligatures" class=""></span></div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; min-height: 13px;" class="">However, when the sequence is empty the method returns true, which is not the desired&nbsp;behaviour.</div>
<div style="margin: 0px; font-size: 11px; line-height: normal; font-family: Menlo; min-height: 13px;" class="">
<br class="">
</div>
<div style="margin: 0px; line-height: normal; min-height: 13px;" class="">
<div style="margin: 0px; line-height: normal;" class="">
<div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class=""> a = [</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">Int</span><span style="font-variant-ligatures: no-common-ligatures" class="">]()</span></div>
<div style="font-family: Menlo; font-size: 11px; margin: 0px; line-height: normal;" class="">
<span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">let</span><span style="font-variant-ligatures: no-common-ligatures" class="">&nbsp;b =&nbsp;a.</span><span style="font-variant-ligatures: no-common-ligatures; color: #587ea8" class="">all</span><span style="font-variant-ligatures: no-common-ligatures" class="">(</span><span style="font-variant-ligatures: no-common-ligatures; color: #de348c" class="">where</span><span style="font-variant-ligatures: no-common-ligatures" class="">:
 { $0 &gt; 7 })</span></div>
<div style="margin: 0px; line-height: normal;" class=""><span style="color: rgb(88, 126, 168); font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">XCTAssertFalse</span><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class="">(b)
 &nbsp;&nbsp;</span><font color="#c0c0c0" face="Menlo" class=""><span style="font-size: 11px;" class="">// This fails, cause there’s no guard&nbsp;against an empty sequence</span></font></div>
</div>
<div style="font-family: Menlo; font-size: 11px;" class=""><br class="">
</div>
<div class=""><span style="font-family: Menlo; font-size: 11px; font-variant-ligatures: no-common-ligatures;" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Does anyone know how to&nbsp;</span></span>guard against an empty sequence<span style="font-family: Helvetica; font-size: 12px;" class="">?</span></div>
<div style="font-family: Menlo; font-size: 11px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class="">
</span></span></div>
<div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">I’m using Xcode 7.3.1 and Swift 2.2.</span></div>
<div style="font-family: Menlo; font-size: 11px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class="">
</span></span></div>
<div style="font-family: Menlo; font-size: 11px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class="">Best,</span></span></div>
<div style="font-family: Menlo; font-size: 11px;" class=""><span style="font-variant-ligatures: no-common-ligatures" class=""><span style="font-family: Helvetica; font-size: 12px;" class=""><br class="">
</span></span></div>
<div class=""><span style="font-variant-ligatures: no-common-ligatures;" class="">— A</span></div>
</div>
</div>
_______________________________________________<br class="">
swift-users mailing list<br class="">
<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a><br class="">
https://lists.swift.org/mailman/listinfo/swift-users<br class="">
</div>
</blockquote>
</div>
<br class="">
</div>
</body>
</html>