Result

As we’re approaching the end of our 2015 iOS Advent Calendar the library I chose for today’s tutorial has a practical use, but mainly wants to be inspirational.

Result by Rob Rix is an implementation of the Result monad in Swift.

This type is similar to the standard Swift’s Optional, but carries more information with it:

enum Result<T, Error: ErrorType> {
  case Success(T)
  case Failure(Error)
}

As you can see result has a Success and a Failure state, both with an associated value. Success can hold a generic value T, while Failure an error.

How to use Result in Swift

Result can be used as the return type of a function executing an operation that can either succeed or fail, for example:

func somethingThatCanFail() -> Result<String, NSError>

switch somethingThatCanFail() {
case .Success(let string):
  print(string)
case .Failure(let error):
  print(error.localizedDescription)
}

As you can see this is quite similar to Swift throw, but Result allows us to leverage higher order functions like map and flatMap.

For example, say we have a function fetching a JSON describing a pizza:

func fetchJSON() -> Result<[String: AnyObject], NSError>

And another function that given a JSON returns a pizza:

func pizzaWithJSON(JSON: [String: AnyObject]) -> Pizza

We can combine the two using map, obtaining an object of type Result<Pizza, NSErorr>:

let pizzaResult = fetchJSON().map(pizzaWithJSON)

We can then switch on this result like any other:

switch pizzaResult {
case .Success(let pizza):
  servePizza(pizza)
case .Error(let error):
  apologizeToCustomerWithError(error)
}

The power of this approach is in the function composition enabled by Result implementing map, flatMap, and other higher order functions. This enables you to build your software as a collection of small, self-contained, and easy to reason about functions and then compose them together.

Next Steps

As I said at the start, with this little article I hope to inspire you to think outside the conventional object oriented box, and start exploring the possibilities offered by other paradigms like functional programming.

If you want to find out more about Result and how to use it head over to the project’s page on GitHub.

The library’s author has published a number of other implementation of functional concepts in Swift, which you can find at antitypical.github.io.

If you want to experiment more, Swiftx and Swiftz are bigger libraries implementing many functional programming features.

Functional Swift is a great book to learn about functional programming and see it applied to everyday iOS development in Swift.

Maybe Haskell is another great book, that focuses on the Maybe Haskell type, which is the equivalent of Swift’s Optional. This book helped me a lot understanding the concept of functor and monad, and their application via map and flatMap.

The videos from the last Functional Swift Conf have just been released, and you can also watch the talks from last year.

Finally you might want to use your functional skills in a reactive approach. Functional Reactive Programming, FRP for short, is a programming approach where almost everything is modelled as “a stream of signals over time”, on which functional operations like map, flatMap, throttle, zip, concat, etc. can be applied. ReactiveCocoa, and RxSwift are two frameworks implementing these concepts.


That’s it for today. See you tomorrow with a bag of UI treats. Subscribe to the email list to avoid missing out.

If you found this post useful and want to support the Advent Calendar please consider sharing it on your favourite social network. Thanks 🎅