Using monads in Swift
One of the coolest features of Swift is that it lets you define your own operators. This leads to being able to re-implement many operators found in functional languages that we know and love.
Swiftz is a new library that implements a handful of essential and obscure
operators, as well as many structs that we take for granted in functional
environments. Today I’m going to take a brief look at the Maybe
monad provided
by Swiftz.
Let’s implement a game of chinese whispers. But in this version, any participant can give up on the game. We have the participants, each a function that might return a phrase that they think is correct, or nothing at all:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Now, how do we write a function that takes a phrase and an ordered list of participants and then returns the result? We could do this:
1 2 3 4 5 6 7 8 |
|
You’ll notice that we are checking for isNone()
on each iteration because we
don’t want to pass the next participant anything if the game has already
stopped. This is a problem that Monad’s solve, given the context of Maybe
it
knows how to handle the case of nothing or something:
1 2 3 4 5 6 7 8 9 |
|
Okay. That saved about one line, but we can make the entire thing more functional:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|