September 2018
Intermediate to advanced
398 pages
9h 43m
English
In the section cats.Apply about Apply, you saw that we can combine many values in an F context by using mapN. However, what if the values that we want to combine are in a collection instead of a tuple?
In that case, we can use the Traverse type class. Cats provide instances of this type class for many collection types, such as List, Vector, and SortedMap.
The following is a simplified definition of Traverse:
@typeclass trait Traverse[F[_]] extends Functor[F] with Foldable[F] with UnorderedTraverse[F] { self => def traverse[G[_]: Applicative, A, B](fa: F[A])(f: A => G[B]): G[F[B]] (...) }
This signature means that I can call it with a collection, fa: F[A] (for instance, Vector[String]), and a function that takes A and returns ...
Read now
Unlock full access