- Learning Functional Programming in Go
- Lex Sheehan
- 282字
- 2021-07-02 23:13:49
The big reveal
A monad chains continuations.
Recall the monad from the hierarchy diagram of Fantasy Land algebras earlier in this chapter?
We'll talk a lot more about Monads in the last unit of our book, but for now let's take a sneak peak at the big picture.
Earlier we saw composition of functions:
![](https://epubservercos.yuewen.com/6176FA/19470400908922906/epubprivate/OEBPS/Images/Chapter_179.jpg?sign=1739054725-kb9n7eVos0oPpGauP2xdxHcbdPpyDopB-0-73afc5f262e18bf5d0d337101a27b287)
That's actually a problem because that's not a Monoid. A Monoid looks like this:
![](https://epubservercos.yuewen.com/6176FA/19470400908922906/epubprivate/OEBPS/Images/Chapter_37.jpg?sign=1739054725-J8d9QF08lL9pYYCp5ljXWBm9YiqcC2U7-0-3286b12daab7069472073b8b714b70b8)
And that's the big reveal. Monads are purple!
Ha. Gotcha!
Besides the color, what can you see that's different between the monadic function and the ones above it?
What about the a going in and the a coming out? That means that if a Monoid accepts a parameter of type A (by convention, a lower case a variable is a value of type A), then it will spit out another a value.
Guess what that's called? When our function returns the same type that it's fed? We call that an endomorphism where en means same and morphism means function; So, it changes from an a to an a. Simple.
What about the chain word used in the a monad chains continuations statement?
How about a nice monoidal purple chain of functions?
![](https://epubservercos.yuewen.com/6176FA/19470400908922906/epubprivate/OEBPS/Images/Chapter_367.jpg?sign=1739054725-RoClmyFSvbHS406hPs1Toor6jviKzbAw-0-bc4f0d3b3c2cf8ad6fa80ee1c20b447c)
What else do we know about this purple monoid chain?
![](https://epubservercos.yuewen.com/6176FA/19470400908922906/epubprivate/OEBPS/Images/Chapter_47.jpg?sign=1739054725-HlxFKYMUolZLifDFxleghhFZuQIOa0gW-0-aa57a5e0113c4298e7d47ba113265589)
If all functions are monids then we can combine them in any order (associativity rule).
Great, but what can we do with a Monoid chain? Can we run the processes in parallel?
Run in parallel? Well, that depends on what we're dealing with. Many things can run in parallel.
In theory, yes but in practice we'll need to deal with the same considerations other Map/Reduce solutions such as Hadoop must deal with.