-
-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect description of flatMap (Appendix B) #210
Comments
The intent is to show that That usage is more for the purposes of either debugging or for connecting FP code to non-FP code. As such, it's not as relevant what the typical/implied type signatures of This isn't a monadic law violation, even if it's atypical. FWIW, in my more recent monad uses and the library I wrote, I provide |
The Difference between const a = Just.pure(5)
const fn1 = (value) => Just.pure(value + 3)
const fn2 = (value) => value + 3
// map
a.map(fn1) // result: Just(Just(8))
a.map(fn2) // result: Just(8)
// flatMap
a.flatMap(fn1) // result: Just(8)
a.flatMap(fn2) // incorrect - fn2 doesn't return Just monad. It can't be used here |
This is also shown in the fantasyland you mentioned in the book: https://github.com/fantasyland/fantasy-land#fantasy-landchain-method
|
The monad laws do not imply a type signature. The FL spec is an interpretation of those laws into JS, but they're not the authority that gets to tell everyone else how their methods should look. I don't care to be FL-compliant, so I don't have any need to fit into their type signature. The monad laws exhibit/require a set of capabilities. The monad I've described in this book is able to do all the things that the monad laws require. The fact that it can also do something else non-monadic doesn't invalidate its monad-ness. |
Moreover, I remind you that you're focused on an appendix to a book written over 5 years ago. The views presented there are how I felt about monads 5 years ago, when I was very early on in my journey. I have a more well-rounded perspective on monads now, and my current explorations of them do not involve using |
This comes from the monad laws. First example to explain the monad laws (note that in Haskell I feel that you perceive this comment as an attack, while I wanted to highlight the error (I still claim that there is an mistake in the book) so that this can be corrected in future releases. Currently it misses a very important feature of the monad which is the composition of functions for sequential data processing with the possibility to fail fast at the first failure. |
You are entitled to the opinion that what's presented there is an "error", but I just don't happen to agree. It's certainly an alternative (and additional, less mainstream) point-of-view about For the sake of clarity (posterity reading this), here's the relevant section from the appendix:
So what I said was:
I stand by that characterization. The appendix is not in any way a full dissertation on monads or the fullness of everything that these capabilities provides. I readily admit that at the time of writing, I didn't really appreciate most of the utility/importance of monads, which is why this content was placed in an appendix as sort of "bonus" material rather than the main content of the book. So the fact that I don't cover, in more depth, what I have, more recently, written a much more complete explanation of my perspective on monads, including detailed coverage of the point of the Were I to write another book on FP and to cover monads, this guides reflects how I would cover it, much more so than the 5 year old appendix we're discussing presently. But what it comes down to is, I don't think what I said back then was an error. I think it was limited and naive because at the time I didn't understand or appreciate much about monads, and now I understand and appreciate them more. However, neither then nor now, do I choose to constrain my perspective on the utility of monads with type signatures (in JS or any other language). Some people find comfort in the type signatures. I do not. |
Yes, I promise I've read the Contributions Guidelines (please feel free to remove this line -- if you leave this line here, I'm going to assume you didn't actually read it).
I think Appendix B incorrectly describes the
flatMap
(chain
,bind
) monad method. From the description, this method is used to leave the monad (as an example, theidentity
function was used to return a value without a "wrapper"). However, the monad does not actually expose any method for doing this.The
flatMap
method is used to combine subsequent monads together in a sequential manner, which is the main strength of monads.The flatMap function signature could look like this (
A
is a type of current value,F
is the monad itself):Therefore, the
identity
function cannot be used insideflatMap
. In my opinion, the following is a valid example of usingflatMap
:The text was updated successfully, but these errors were encountered: