Copy and paste the swift code below into a playground to experiment.
This is a very close emulation of Functor and Monad typeclasses in swift. However, it is very fragile (i.e. easy to crash the compiler).
For example, instance methods of fmap
will run fine, but attempting to use a globally defined fmap
that acts on Functor
types will cause a crash. Similarly for bind
. Unfortunately this means we cannot define the nice infix operator versions of these functions.