-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add Traverse.traverseM #1020
Add Traverse.traverseM #1020
Conversation
* A traverse followed by flattening the inner result. | ||
*/ | ||
def traverseM[G[_], A, B](fa: F[A])(f: A => G[F[B]])(implicit G: Applicative[G], F: FlatMap[F]): G[F[B]] = | ||
G.map(traverse(fa)(f))(F.flatten) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We got F
(FlatMap[F]
) as an Applicative[F]
, but we could also use the Traverse
itself : G.map(traverse(fa)(f)(this))(F.flatten)
.
I am not sure if there is a reason to choose one over the other ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the version that you currently have is probably better, because Applicative.traverse
ends up delegating through to Traverse.traverse
anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be called traverseA
? Consistent with replicateA
. I think the M
suffix is usually used when it has a Monad
constraint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adelbertc The Applicative[G]
is required on traverse
also. Here I think of the M
as "monadic", referring to the FlatMap[F]
. I guess we could call it traverseF
instead, though that runs the risk of being confused with Functor
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not really sure about the M
. traverseA
seems a bit strange because we need FlatMap
. traverseF
could also be confusing with the flatMapF
and mapF
on (some) monad transformers.
Maybe we can call it traverseFlatten
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I totally did not see the FlatMap
requirement.. the size of my screen stopped right at Applicative
so I assumed that was all there was. I am good with traverseM
in that case
👍 thanks! |
👍 |
I have added
Traverse.traverseM
and added some examples to thetraverse
syntax.