Mark Elston (114) [Avatar] Offline
#1
You say that Map[K, V] is a monoid if V is a monoid. Here BigDecimal is a monoid. Is this supposed to be well understood, already? What makes this so? Is that also why you can get the zero value for the constructor use in the definition of zeroMoney?

I am very confused by the use of the implicitly function in this listing. I have not come across an explanation of this that I can make sense of. In fact, the whole area of Scala implicits is one that leaves me wanting to tear out what little hair I have left. What this appears to be saying is that m is a Monoid[Map[Currency, BigDecimal]]. However, I don't have a feel for what that implies in terms of the zero and op definitions for this monoid. Where are they defined? How do we know what they do? This seems to be an important point since the definition of op for Monoid[Money] makes use of m.op without it ever being defined anywhere. How can we reason about this operation without knowing what it does? I also assume that the use of m1.m and m2.m in the definition of op refer to the m vals declared in the Money case class and not the implicit m defined above.

In listing 4.3 you again use implicits but you go on to say that "In Scala, you need to pass an implicit object explicitly with every function that needs it.". What do we need implicits for if we need to explicitly pass them to the functions? Also, in the following examples I don't see the proper Monoid ever passed explicitly so I don't understand at all what you are getting at. Unless you mean that you are passing them in the function definitions and not the call sites. But that is the only way I have seen implicits used, anyway. So what point are you trying to make? Also, I can't find the term explicit dictionary-passing technique used anywhere else online. Is this a relatively new term?
Mark Elston (114) [Avatar] Offline
#2
I'm following up on this as I had to go to StackOverflow to get a clue how to answer this. So, if anyone else comes along and has the same problem, here is the answer (as best as I can describe it).

The note on page 113 that Map[K,V] is a monoid if V is a monoid is a bit misleading (at least it was to me). I assumed that this was true due to something intrinsic about Map or some automatically available implicit I was unaware of. It turns out that the author's code base provides an implementation of Map[K,V] (it actually provides two of them). The implementation of this monoid is just not discussed in the text of the book. I was staring at the text of the book and trying to make sense of what I was reading and couldn't do it because I didn't have all the information I needed.

Going back to the text on page 113 I see the statement that "you need to first define Monoid[Map[K,V]] and then..." I didn't pick up on the fact that this was the only mention of this definition the author was going to make and thought it was somehow provided in the listing below it. That was where I got lost. I didn't see how this monoid was being defined in the code in the book. I was right, it wasn't. It was defined elsewhere. I just didn't know where to go to find it.

If you are interested it is in the code for chapter 4 in the patterns/monoids/foldable/Monoid.scala and patterns/monoids/monoid/Monoid.scala files.