Recorded – watch the full episode on YouTube.
How would you define a monad to a Swift developer, particularly one who's heard many academics attempt to explain it like burritos and really walked away clueless?
Daniel Steinberg: I think it was Chris Eidhof who told me, “to understand monads, you have to give a talk on monads.” And then his second thing was, "There is this divide. And at the point that you understand monads, you don't remember what it was not to understand monads and so you're not good at explaining it."
“So every monad is a functor, and now you don't have to take category theory.”
Let's start with a functor, a functor is any type that supports map. So if you support map, you're a functor. And essentially, there's only a little lie in what I'm about to say, essentially, any type that supports
flatMap() is a monad. Now, it's
flatMap() and the thing that embeds – some people call it Pure, but in Combine, it's called
Just. It's a thing that takes a string and wraps it in an array of strings or a string and wraps it in an optional string. That's called
Just. So if you're a type that supports
Just or sometimes called pure, if it supports that and
flatMap(), it's a monad. And there's a very pretty picture that shows whenever you support
flatMap(), you automatically support map. So every monad is a functor, and now you don't have to take category theory.
Paul Hudson: But would you say, if folks wanted to get really serious about this kind of thing, they might think, “well, listen, there are the three monadic laws.” They matter but are they important, do you think? Do you have to know them? Or is it more about understanding the shape of what it's trying to do?
Daniel Steinberg: This goes back to another great talk I saw by TJ Usiyan. He talked about the double equals. And he said, “double equals is defined.” But if you look at where double equals is defined for equatable in comments, it says if you are doing equality, it's got to be anything is equal to itself. If A is equal to B, then B is equal to A, right? That's the reflexive. And also, if A equals B, and B equals C, A equals C, the transitive. But you can't enforce that in code. I can't make double equals do that. I'm just telling you that when you implement double equals, you should follow all that.
“I lift this function up to go from array to array and this function up to go from array to array, it doesn't matter if I compose the two functions here and then lift it up two arrays, or I go up here and I go across the composition of the mapped functions. That's one of the functor laws. So the monadic laws are very similar.”
And it's kind of that with the monadic laws, I can't make you create a
flatMap() that does the right thing but it's sort of the contract that you should follow those laws. And so we usually think in terms of diagrams and I don't have a whiteboard behind me, thank goodness. But for instance, in math the idea is, imagine that I have a function from string to integer, and another function from integer to double. Now I have an array of strings. And so the idea is, I lift this function up to go from array to array and this function up to go from array to array, it doesn't matter if I compose the two functions here and then lift it up two arrays, or I go up here and I go across the composition of the mapped functions. That's one of the functor laws. So the monadic laws are very similar.
Paul Hudson: Yeah. Here’s an easy listener question for you then: “using that logic, is array a monad?"
Daniel Steinberg: Well, yes. You got to be careful because… this is such a math thing for me to say. You can say the positive integers are a group, if you know what a group is in math, but that's not really correct. What's correct is to say the integers with the plus operator is a group. You have to specify what the operator is and usually what the identity is. So to say “array is a monad,” array as it's implemented in Swift is a monad because that array includes all the things a monad needs. It has map, it has
flatMap() and they obey the laws. So shorthand we say an array is a monad, but that's only because the people that wrote the Swift standard library made it so.
Paul Hudson: Right. Exactly. Also therefore a functor?
Daniel Steinberg: Yes.
Paul Hudson: So folks, you can now go to the next meetup and blow folks away saying, "Yeah, I think we need more monads." And what you mean, an array really, but you can sound super smart at the same time.
Daniel Steinberg: And if you're saying that's your easy question, I dread when you get to the intermediate ones.
This transcript was recorded as part of Swiftly Speaking. You can watch the full original episode on YouTube, or subscribe to the audio version on Apple Podcasts.
SPONSORED From August 2nd to 8th you can join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer!
Link copied to your pasteboard.