The names of the methods

In the preceding section, we mentioned that it doesn't actually matter how the unit method is called. While it is true for unit and it could be propagated to any of the other methods, it is recommended that map and flatMap actually remain this way. It doesn't mean that it is not possible to make things work, but following common conventions would make things much simpler. Moreover, map and flatMap give us something extra—the possibility of using our classes in for comprehensions. Consider the following example, which is only here to illustrate how having methods with such names helps:

case class ListWrapper(list: List[Int]) {  // just wrap  def map[B](f: Int => B): List[B] = list.map(f)  // just wrap  def flatMap[B](f: ...

Get Scala Design Patterns - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.