Working on an exercise from Functional Programming in Scala, I implemented a Traverse instance for Option:
override def traverse[G[_],A,B](oa: Option[A])(f: A => G[B])(implicit G: Applicative[G]):
G[Option[B]] =
oa match {
case None => G.unit(None)
case _ => {
val a: A = oa.get
val x: G[B] = f(a)
G.map(x)(Some(_))
}
}
Is this idiomatic? Besides condensing the case _'s 3 lines to 1, perhaps there's a more concise way to write this method?
G.unit(oa map f)? \$\endgroup\$oa map fproduces a type ofOption[G[B]]. ApplyingG.unitwould giveG[Option[G[B]]. \$\endgroup\$