Image for post
Image for post
Thanks to Lukas for the illustration

I have been spending a good portion of my time studying SICP. SICP avoids focusing on the terms such as Functor (Map) and Monad (FlatMap) and instead focuses solely on their implementations and the power they provide us.

You can find all the code below in the following playground:

The implementations are surprisingly simple. Look at the implementation of Optional’s Map in Swift’s very own source code.

public func map<U>(
_ transform: (Wrapped) throws -> U) rethrows -> U? {
switch self {
case .some(let y):
return .some(try transform(y))
case .none:
return .none
}
}

Most developers I know make heavy use of Map and FlatMap. I do think it is of utmost importance to understand their implementations in order to understand how they actually work across different Types. As an exercise try code Array’s implementation of Map and FlatMap before reading further.


Image for post
Image for post

I have been reading Professor Frisby’s Mostly adequate guide to Functional Programming. A great read, and a book I would certainly recommend. One issue the author highlights is how do we deal with side-effects effectively. Most software engineers are familiar with functors such as Arrays, Optionals, Results, and Promises. There exists another type designed with the intent to deal with side effects. This is the IO type.

You can find all the code for the article in the following playground file.

First let’s take a look at an example of an impure function.

Image for post
Image for post
https://github.com/TYRONEMICHAEL/io-type-swift

Nothing in the function signature indicates that this is indeed a function which has a side-effect. But we can make the function pure by simply making a minor change to the function signature, (Int) -> () -> String?, …


Image for post
Image for post
Photo by Genessa Panainte — What comes to mind when thinking of the publish-subscribe pattern

Breaking your app up into modules has many benefits and it is extremely easy to achieve with Xcode using workspaces or Cocoapods. Whether you are working with feature teams or you have a set of reusable modules shared between different applications, one of the design decisions you will need to make is how to communicate between the various modules.

I have never been a fan of using a publish-subscribe pattern because completely decoupled events are harder to trace, debug, and maintain. When our architect suggested using a plugin architecture and having worked with Wordpress earlier in my career, I became quite excited.

About

Tyrone Michael Avnit

Polyglot Programmer. Specialising in iOS development. Liverpool supporter & Comrades Marathon runner. Speaker & iOS engineer @civickey.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store