From 0c9bf1526d5678784a2f7d5067d37219dc10f2e5 Mon Sep 17 00:00:00 2001 From: Ben Koshy Date: Tue, 15 Oct 2019 08:40:43 +1100 Subject: [PATCH] Add explanation re: code example #219 This PR seeks to clarify any queries that other readers may have re: the above code. I hope it is helpful to you. Thanking @fdbeirao for the contents of this commit. regards Ben --- book/effects/random.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/book/effects/random.md b/book/effects/random.md index 9e73b25a..ccb419e1 100644 --- a/book/effects/random.md +++ b/book/effects/random.md @@ -163,6 +163,20 @@ From there we use `map3` to combine them into a new `spin` generator. It says to The point here is that from small building blocks, we can create a `Generator` that describes pretty complex behavior. And then from our application, we just have to say something like `Random.generate NewSpin spin` to get the next random value. +But wait, you may be thinking, why is `Cherry` outside of the list? Consider the below: + +```elm +symbol = + Random.uniform Cherry [ Seven, Bar, Grapes ] -- Why is Cherry not in the list?! +``` +Well, consider what happens if the list is empty?: + +```elm +symbol = + Random.uniform [ ] -- The list is empty +``` +Oops! That looks like a grenade waiting to detonate! If that's the case, then we would have to return a `Maybe Generator` or `Generator Maybe a`, to handle that situation - and this adds some more complexity to our code. In other words, we want to force developers to provide for a default value, in this case: `Cherry` just in case the list is empty. If we can guarantee that, we can stop the problem at its root source, we and have a more pleasant API to deal with. + > **Exercises:** Here are a few ideas to make the example code on this page a bit more interesting! >