<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>
<blockquote type="cite" class="">
<div class="">On May 9, 2016, at 6:18 AM, Adriano Ferreira &lt;<a href="mailto:adriano.ferreira@me.com" class="">adriano.ferreira@me.com</a>&gt; wrote:</div>
<div class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div class=""><br class="">
</div>
<div class="">So, I thought about “underestimatedCount” but was not sure how to use it properly.</div>
</div>
</div>
</blockquote>
<div><br class="">
</div>
<div>`guard self.underestimateCount &gt; 0 else {return false}`</div>
<br class="">
<blockquote type="cite" class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div class="">Also, about using “next”, Austin mentioned something interesting:</div>
<div class=""><br class="">
</div>
<div class=""><i class="">“…&nbsp;a sequence might actually be a sequence of&nbsp;n&nbsp;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&nbsp;generator.”</i></div>
</div>
</blockquote>
<div><br class="">
</div>
<div>the `for … each` enumeration in the body of your function also consumes the elements of your sequence &nbsp;:) &nbsp;so I assumed that was an acceptable consequence of applying this function</div>
<div><br class="">
</div>
<div>if you’re thinking specifically about checking the first value returned by `next()` for nil and then no longer having it available to test against your predicate, then I was thinking roughly along the lines of something like:</div>
<div><br class="">
</div>
<div>```</div>
<div>var generator = self.generate()</div>
<div>
<div>var isPredicateSatisfiedForAllElements = false</div>
<div>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;</div>
<div>while let element = generator.next() {</div>
<div>&nbsp;&nbsp;isPredicateSatisfiedForAllElements = try predicate(element)</div>
<div>&nbsp; if !isPredicateSatisfiedForAllElements {break}</div>
<div>}</div>
<div><br class="">
</div>
<div>return isPredicateSatisfiedForAllElements</div>
</div>
<div>```</div>
<div><br class="">
</div>
<div>— Shane S</div>
<div><br class="">
</div>
<br class="">
<blockquote type="cite" class="">
<div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">
<div class=""><br class="">
</div>
<br class="">
<div class="">
<blockquote type="cite" class="">
<div class="">On May 8, 2016, at 11:42 PM, Shane S &lt;<a href="mailto:electro_alchemy@hotmail.com" class="">electro_alchemy@hotmail.com</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="">
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 &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="">
<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="">
</div>
</blockquote>
</div>
<br class="">
</body>
</html>