[swift-users] Guarding against an empty sequence

Shane S electro_alchemy at hotmail.com
Mon May 9 10:21:19 CDT 2016


On May 9, 2016, at 6:18 AM, Adriano Ferreira <adriano.ferreira at me.com<mailto:adriano.ferreira at me.com>> wrote:

So, I thought about “underestimatedCount” but was not sure how to use it properly.

`guard self.underestimateCount > 0 else {return false}`

Also, about using “next”, Austin mentioned something interesting:

“… 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.”

the `for … each` enumeration in the body of your function also consumes the elements of your sequence  :)  so I assumed that was an acceptable consequence of applying this function

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:

```
var generator = self.generate()
var isPredicateSatisfiedForAllElements = false

while let element = generator.next() {
  isPredicateSatisfiedForAllElements = try predicate(element)
  if !isPredicateSatisfiedForAllElements {break}
}

return isPredicateSatisfiedForAllElements
```

— Shane S




On May 8, 2016, at 11:42 PM, Shane S <electro_alchemy at hotmail.com<mailto:electro_alchemy at hotmail.com>> wrote:

I imagine `#underestimateCount()` is going to be your best bet, though you may not always see the results you desire

some notes:

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

2. do you have the option to constrain this to CollectionType instead?

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

— Shane S


On May 8, 2016, at 7:16 PM, Adriano Ferreira via swift-users <swift-users at swift.org<mailto:swift-users at swift.org>> wrote:

Hi everyone!

I’m working on the following method:

extension SequenceType {

    /// Check if `predicate` is true for all elements of `self`
    ///
    /// - Parameter predicate: The predicate called on each element of `self`
    ///
    /// - Returns: True iff every element in `self` satisfies `predicate`, false otherwise

    @warn_unused_result
    func all(@noescape where predicate: Generator.Element throws -> Bool) rethrows -> Bool {
        for element in self where try !predicate(element) {
            return false
        }

        return true
    }
}

However, when the sequence is empty the method returns true, which is not the desired behaviour.

let a = [Int]()
let b = a.all(where: { $0 > 7 })
XCTAssertFalse(b)   // This fails, cause there’s no guard against an empty sequence

Does anyone know how to guard against an empty sequence?

I’m using Xcode 7.3.1 and Swift 2.2.

Best,

— A
_______________________________________________
swift-users mailing list
swift-users at swift.org<mailto:swift-users at swift.org>
https://lists.swift.org/mailman/listinfo/swift-users



-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160509/2e5b6b79/attachment.html>


More information about the swift-users mailing list