Advanced Swift: Generics and Protocols · Challenge: Generics | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1940187-advanced-swift-generics-and-protocols/lessons/5

Hi Ray,
thanks for the insightful tutorials, it’s a very interesting topic!

I’m playing around with the Distribution protocol. And if I understand correctly I should now be able to use the sample function with my own struct conforming to the RandomGenerator protocol.
Let’s say I create 2 different custom generators

struct CustomGenerator1: RandomNumberGenerator {

    func next() -> UInt64 {
        return UInt64(2)
    }

}

struct CustomGenerator2: RandomNumberGenerator { }

Both of which conform to the RandomNumberGenerator protocol (as it already supplies a default implementation for the next() function).

Then I should be able to ‘roll the dice’ like so

 let d6 = UniformDistribution(range: 1...6)
 var g1 = CustomGenerator1()
 var g2 = CustomGenerator2()
 d6.sample(using: &g1)
 d6.sample(using: &g2)

however, the playground doesn’t output a number.
In the first case, using g1, it seems to get stuck in a loop. And the second case will eventually produce an error or crash.

Any idea if I’m doing something wrong?

Thank you,
Filip

@filipd Do you still have issues with this?

Hi @rayfix
Thank you for the tutorial. I have a question here.
In the method below shouldn’t we be using the passed in value of generator rather than creating our own SystemRandomNumberGenerator() otherwise the user cannot use his custom generator with the ‘sample count’ method?

func sample<G: RandomNumberGenerator>(count: Int, using generator: inout G) → [Value] {

var g = SystemRandomNumberGenerator()

return (1…count).map { _ in sample(using: &g) }

}

I tried to modify it this way -
func sample<G: RandomNumberGenerator>(count: Int, using generator: inout G) → [Value] {

return (1…count).map { _ in sample(using: &generator) }

}

Thank You

1 Like

Oh. Good catch! I think that was a cut and paste error from the overload that uses the system generator. Thanks for reporting it.

@rayfix Thank you for taking the time to create these materials related to protocol-oriented programming.

I would like to provide some feedback relative to the context in which you’ve chosen to teach these more advanced Swift concepts. By combining advanced Swift topics with advanced mathematical (and statistical) concepts you have exponentially increased the difficulty of the ability of consumers to understand the Swift concepts. For those of us who aren’t that skilled with math and/or statistics, it makes it exceedingly difficult to understand the Swift concept being taught and the complexity of the non-Swift context is very distracting. In the future, please consider using very simple contexts and examples in which to teach Swift concepts, so that the focus can remain on only the Swift in a way that the consumer does not have to rely on also understanding one or more significantly more complicated subjects that are far outside and away from Swift.