简体   繁体   中英

Haskell's mapM equivalent for Scala cats

Does cats provide a function equivalent to mapM in Haskell? It should look like:

  def mapM[A, B, F[_], Col[_]]
  (col: Col[A])(f: A => F[B])(implicit F: Applicative[F], T: Traverse[Col]): F[Col[B]] = 
    T.sequence[F, B](F.map(col)(f))

Unfortunately I haven't found any function like this one =(

It's called traverse :

scala> import cats.implicits._
import cats.implicits._

scala> List(1, 2, 3).traverse(n => Option((n * 2).toString))
res1: Option[List[String]] = Some(List(2, 4, 6))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM