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.
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.
@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]].
*/| } | ||
| } | ||
|
|
||
| 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.
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.
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.
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
armanbilge
left a comment
There was a problem hiding this comment.
LGTM! Thanks for picking this up :)
zipWithLongIndex, mapWithLongIndex and traverseWithLongIndexMzipWithLongIndex, mapWithLongIndex and traverseWithLongIndexM
Implements #4245.