Skip to content

Instantly share code, notes, and snippets.

@philipschwarz
Created August 24, 2020 18:05
Show Gist options
  • Save philipschwarz/2d94a3ae0197d846402d1fd09f400505 to your computer and use it in GitHub Desktop.
Save philipschwarz/2d94a3ae0197d846402d1fd09f400505 to your computer and use it in GitHub Desktop.
flatten is indeed an interesting topic - one cool thing is that flatMap is just map followed by flatten, i.e. (ma flatMap f) == (ma map f flatten) [1].
Another is that flatten is flatMap(identity) [2][3].
Another is that a monad can be defined as a functor with a unit function (i.e. a type constructor like List or Option) and a flatten function (aka join in Haskell) [4]
[1] https://github.com/philipschwarz/scala-fp-combinators-code-kata/blob/master/src/main/scala/FPCombinatorsCodeKata.scala#L18 - https://www.slideshare.net/pjschwarz/scala-functional-programming-combinators-code-kata
[2] https://github.com/philipschwarz/scala-fp-combinators-code-kata/blob/master/src/main/scala/FPCombinatorsCodeKata.scala#L20
[3] https://www.slideshare.net/pjschwarz/symmetry-in-the-interrelation-of-flatmapfoldmaptraverse-and-flattenfoldsequence
[4] https://www.slideshare.net/pjschwarz/monad-as-functor-with-pair-of-natural-transformations#8
Sorry, I got a bit overexcited - the above is probably way too premature in the context of this post, and/or something that you may anyway be building up to in this series.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment