Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding sequenceM method to Traverse #1394

Merged
merged 3 commits into from
Oct 24, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions core/src/main/scala/cats/Traverse.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ import simulacrum.typeclass
def sequence[G[_]: Applicative, A](fga: F[G[A]]): G[F[A]] =
traverse(fga)(ga => ga)

/**
* Thread all the G effects through the F structure and flatten to invert the
* structure from F[G[F[A]]] to G[F[A]].
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> val x: List[Option[List[Int]]] = List(Some(List(1, 2)), Some(List(3)))
* scala> val y: List[Option[List[Int]]] = List(None, Some(List(3)))
* scala> x.sequenceM
* res0: Option[List[Int]] = Some(List(1, 2, 3))
* scala> y.sequenceM
* res1: Option[List[Int]] = None
* }}}
*/
def sequenceM[G[_], A](fgfa: F[G[F[A]]])(implicit G: Applicative[G], F: FlatMap[F]): G[F[A]] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sequenceA? since applicative

Copy link
Contributor

@kailuowang kailuowang Sep 30, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well this one is corresponding traverseM, maybe both should be renamed? (if we ended up doing that we need to add the "breaking change" label. ) Also, maybe implementing this with traverseM would make the connection more obvious?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about flatTraverse or flatSequence, taking the convention from Scala's flatMap? I realize that this isn't well established in other languages, but both traverseM and sequenceM aren't as self explanatory

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a similar conversation when I added traverseM in #1020.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delayed response. I like flatTraverse and flatSequence, they make sense to me.

G.map(sequence(fgfa))(F.flatten)

/**
* Behaves just like sequence, but uses [[Unapply]] to find the
* Applicative instance for G.
Expand Down