Tuesday, January 13, 2015 [Tweets] [Favorites]

Functor and Monad in Swift

Javier Soto:

We’ve seen what map can do when implemented on a container type, like Optional, Array or Result. To recap, it allows us to get a new container, where the value(s) wrapped inside are transformed according to a function. So what’s a Functor you may ask? A Functor is any type that implements map. That’s the whole story.

[…]

In the earlier example, we used the transformation function to return another value, but what if we wanted to use it to return a new Result object? Put another way, what if the transformation operation that we’re passing to map can fail with an error as well?

[…]

And with that, we turned our Result type into a Monad! A Monad is a type of Functor. A type which, along with map, implements a flatMap function (sometimes also known as bind) with a signature similar to the one we’ve seen here.

1 Comment

[…] Functor and Monad in Swift, Higher Order Functions in Swift […]

Stay up-to-date by subscribing to the Comments RSS Feed for this post.

Leave a Comment