In order to make anything operable in monad context, if using Haskell - I just add implementation of class Monad for given type anywhere. So I don't touch a source of the data type definition at all. Like (something artificial)
data Z a = MyZLeft a | MyZRight a
swap (MyZLeft x) = MyZRight x
swap (MyZRight x) = MyZLeft x
instance Monad Z where
return a = MyZRight a
(>>=) x f = case x of
MyZLeft s -> swap (f s)
MyZRight s -> swap (f s)
so I'm not touching definition of Z, but make it as a monad
How do I do this in Scala? It seems that there's no way besides of mixing some traits in and defining methods map/flatMap/filter/withFilter ?