The unit method

The other method we mentioned previously is unit. It actually doesn't matter how this method is called and it could be different for different languages based on their standards. What is important is its functionality. The signature of unit can be written in the following way:

def unit[T](value: T): Monad[T]

What does the preceding line mean? It is pretty simple—it takes a value of the T type and turns it into a monad of the T type. This is nothing more than a single argument constructor or just a factory method. In Scala, this can be expressed using a companion object with an apply method as well. As long as it does the right thing, the implementation doesn't really matter. In Scala, we have many of the collection types as ...

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.