-
-
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
Added zipWithLongIndex
, mapWithLongIndex
and traverseWithLongIndexM
#4247
Added zipWithLongIndex
, mapWithLongIndex
and traverseWithLongIndexM
#4247
Conversation
Nice work!! We should also add some laws similar to these. cats/laws/src/main/scala/cats/laws/TraverseLaws.scala Lines 115 to 131 in 9839b74
Great, but it is a bit more involved so I think a second PR based on this one would be the way to do it :) |
The other thing probably worth doing is adding a method like this one: cats/core/src/main/scala/cats/instances/StaticMethods.scala Lines 51 to 61 in 9839b74
And using it to add a higher-performant override for these instances: |
@armanbilge tried to address the reviews in c1f9ed6 |
/** | ||
* Same as [[traverseWithIndexM]] but the index type is [[Long]] instead of [[Int]]. | ||
*/ | ||
def traverseWithLongIndexM[G[_], A, B](fa: F[A])(f: (A, Long) => G[B])(implicit G: Monad[G]): G[F[B]] = |
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.
Doesn't seem like this should require Monad.
I think I see why this implementation does (I think due to using State).
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.
@johnynek the scaladoc for the Int
version explains:
/**
* Akin to [[traverse]], but also provides the value's index in
* structure F when calling the function.
*
* This performs the traversal in a single pass but requires that
* effect G is monadic. An applicative traversal can be performed in
* two passes using [[zipWithIndex]] followed by [[traverse]].
*/
@@ -60,4 +60,14 @@ private[cats] object StaticMethods { | |||
} | |||
} | |||
|
|||
def mapWithLongIndexFromStrictFunctor[F[_], A, B](fa: F[A], f: (A, Long) => B)(implicit ev: Functor[F]): F[B] = { |
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.
Thanks for adding this! The next step is to use it to provide overrides of mapWithLongIndex
in all the same places that the Int
-version is used.
https://github.com/typelevel/cats/search?q=mapWithIndexFromStrictFunctor
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 be done in 6fe035f. I don't know if I should update the tests too, or these implementations are used automatically somehow.
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.
Not entirely sure of the question :) Actually, the behavior of the implementations should be completely identical, and the tests should be verifying that. Nothing "automatic" going on AFAIK :)
71eb69b
to
6fe035f
Compare
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.
LGTM! Thanks for picking this up :)
zipWithLongIndex
, mapWithLongIndex
and traverseWithLongIndexM
zipWithLongIndex
, mapWithLongIndex
and traverseWithLongIndexM
Implements #4245.