<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=""><div class="">Hi all, thanks for the replies.</div><div class=""><br class=""></div><div class="">So, I thought about “underestimatedCount” but was not sure how to use it properly.</div><div class=""><br class=""></div><div class="">I chose to put this method on SequenceType rather than CollectionType because I’d like it to be less restrictive since there was no need to subscripting.</div><div class=""><br class=""></div><div class="">Also, about using “next”, Austin mentioned something interesting:</div><div class=""><br class=""></div><div class=""><i class="">“… a sequence might actually be a sequence of n random numbers coming out of a PRNG; checking for 'emptiness' by calling 'next' would consume a random number that you couldn't ever get back, even if you called 'next' again using a different generator.”</i></div><div class=""><i class=""><br class=""></i></div><div class="">So far, I have a mixed feeling about using a boolean to track whether or not the loop has even been executed.</div><div class=""><br class=""></div><div class="">I wonder why there’s no simple way (or I’m overlooking it) to check if a sequence is empty.</div><div class=""><br class=""></div><div class="">Any other suggestions?</div><div class=""><br class=""></div><div class="">Cheers,</div><div class=""><br class=""></div><div class="">— A</div><br class=""><div><blockquote type="cite" class=""><div class="">On May 8, 2016, at 11:42 PM, Shane S <<a href="mailto:electro_alchemy@hotmail.com" class="">electro_alchemy@hotmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" class="">
<div 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 class="">
<blockquote type="cite" class="">
<div class="">On May 8, 2016, at 7:16 PM, Adriano Ferreira via swift-users <<a href="mailto:swift-users@swift.org" class="">swift-users@swift.org</a>> 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=""> </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=""> </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=""> </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=""> </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=""> </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=""> </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=""> </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=""> ->
</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=""> ->
</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=""> </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=""> </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=""> }</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=""> </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=""> }</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 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=""> b = 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 > 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)
</span><font color="#c0c0c0" face="Menlo" class=""><span style="font-size: 11px;" class="">// This fails, cause there’s no guard 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 </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="">
<a href="https://lists.swift.org/mailman/listinfo/swift-users" class="">https://lists.swift.org/mailman/listinfo/swift-users</a><br class="">
</div>
</blockquote>
</div>
<br class="">
</div>
</div>
</div></blockquote></div><br class=""></body></html>