Using our functors

A simple example of using our listFunctor that we defined in the preceding section can be seen as follows:

object FunctorsExample {  def main(args: Array[String]): Unit = {    val numbers = List(1, 2, 3, 4, 5, 6)    val mapping = Map(      1 -> "one",      2 -> "two",      3 -> "three",      4 -> "four",      5 -> "five",      6 -> "six"    )    System.out.println(s"The numbers doubled are:     ${listFunctor.map(numbers)(_ * 2)}")    System.out.println(s"The numbers with strings are:     ${listFunctor.map(numbers)(i => (i, mapping(i)))}")  }}

The output of the preceding example is shown in the following screenshot:

As you can see, functors don't really do much by themselves. ...

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.