Skip to content

Instantly share code, notes, and snippets.

@ceedubs
Created September 30, 2014 18:49
Show Gist options
  • Save ceedubs/cd2e11ef9b786066a8ba to your computer and use it in GitHub Desktop.
Save ceedubs/cd2e11ef9b786066a8ba to your computer and use it in GitHub Desktop.
Scalaz traverse example
/* traverse takes an F[A] and an A => G[B] and returns a G[F[B]].
* In this case List[Long] and Long => Option[String] result in Option[List[String]]
*/
scala> val usernames = Map(1L -> "susie", 2L -> "bob")
usernames: scala.collection.immutable.Map[Long,String] = Map(1 -> susie, 2 -> bob)
scala> List(1L, 2L).traverse(usernames.get)
res2: Option[List[String]] = Some(List(susie, bob))
scala> List(1L, 2L, 3L).traverse(usernames.get)
res3: Option[List[String]] = None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment