Added updated to Traverse - #4248
Conversation
ef13cad to
da4ea29
Compare
armanbilge
left a comment
There was a problem hiding this comment.
Do you mind rebasing onto #4247? It looks like both PRs have some of the same commits but with different hashes.
da4ea29 to
dfbc58a
Compare
|
Not sure what I did here, but seems like nothing was lost @armanbilge |
|
Nice job with that! Indeed, it looks right to me :) nikololiahim/cats@traverseWithLongIndexM...nikololiahim:traverseUpdated |
dfbc58a to
83e1275
Compare
armanbilge
left a comment
There was a problem hiding this comment.
Sorry, I didn't realize we are missing a law for this! Also a couple other things I noticed.
| final override def traverse[G[_], A, B](fa: Vector[A])(f: A => G[B])(implicit G: Applicative[G]): G[Vector[B]] = | ||
| G.map(Chain.traverseViaChain(fa)(f))(_.toVector) | ||
|
|
||
| final override def updated_[A, B >: A](fa: Vector[A], idx: Long, b: B): Option[Vector[B]] = |
There was a problem hiding this comment.
We can add a similar override to NonEmptyVector.
| * The behavior is consistent with the Scala collection library's | ||
| * `updated` for collections such as `List`. | ||
| */ | ||
| def updated_[A, B >: A](fa: F[A], idx: Long, b: B): Option[F[B]] = { |
There was a problem hiding this comment.
Sorry, I didn't realize we are missing a law for this! It can be like the other laws, just verifying against the reference implementation.
There was a problem hiding this comment.
@armanbilge I am a little lost here, what should the reference implementation be in this case?
There was a problem hiding this comment.
Ended up adding one of the previous implementations in bdf2d41. Is it okay?
There was a problem hiding this comment.
Ah, the reference implementation should be the same as the default implementation :) basically, it's a way to make sure that if someone overrides it (like we do in Vector) it matches what the default implementation would do.
There was a problem hiding this comment.
Btw, sorry I wrote that so confusingly, I should have just said "default implementation". I was thinking about how these laws end in Ref which I assume stands for "reference".
There was a problem hiding this comment.
Ah, so that's why they are all the same as default. I thought it was weird that they are all just the same as the trait impl. Like, what are we even testing here? The fact that some instance may override certain default impl totally slipped from my head. Now it all makes sense, thank you!
bdf2d41 to
0942803
Compare
armanbilge
left a comment
There was a problem hiding this comment.
LGTM. Great work juggling both PRs!
|
@armanbilge @johnynek Thx for your patience in reviewing/explaining! |
| lhs <-> rhs | ||
| } | ||
|
|
||
| def mapWithLongIndexRef[A, B](fa: F[A], f: (A, Long) => B): IsEq[F[B]] = { |
There was a problem hiding this comment.
@nikololiahim I just realized, we added the laws but not the tests 😅 I completely forgot about those, so sorry!
cats/laws/src/main/scala/cats/laws/discipline/TraverseTests.scala
Lines 74 to 83 in 0942803
There was a problem hiding this comment.
Should I reopen and add them here? Or open a separate PR?
There was a problem hiding this comment.
Whatever works, although I'm not sure if it's possible to re-open a merged PR :) thanks!!
Implements #4246