Yes, you can use traverse
that does just that. If you use cats
<= 0.9.0, you can use the following code:
import cats.data._
import cats.instances.list._
import cats.instances.option._
import cats.syntax.traverse._
// ...
def k(a: String) = Kleisli[Option, Int, Int](m => Some(a.length * m))
val result: Kleisli[Option, Int, List[Int] = List("hi", "hello").traverseU(k)
If you use Scala 2.11.9+ by adding scalacOptions += "-Ypartial-unification"
to your file build.sbt
, you can simply use traverse
instead traverseU
. In addition, starting with version 1.0.0, traverseU
and sequenceU
will no longer exist.
, Scala < 2.11.9, >= 2.10.6 , .