Prepend a Vector to a NonEmptyVector#3362
Conversation
I needed to implement this recently for some work I was doing and was surprised it didn't already exist.
travisbrown
left a comment
There was a problem hiding this comment.
👍, thanks, I think this is a reasonable thing to add, especially since we have something similar for other non-empty collection types (e.g. prependLazyList and prependChain, although not prependList for some reason).
I do think it'd be better to implement it like this:
new NonEmptyVector(vector +: toVector)This is shorter, clearer, more consistent with similar definitions like prepend and concat, and avoids creating an Option and a couple of function values.
| /** | ||
| * Prepend a `Vector` to this, producing a new `NonEmptyVector`. | ||
| */ | ||
| def prependVec[AA >: A](vector: Vector[AA]): NonEmptyVector[AA] = |
There was a problem hiding this comment.
What do you think of naming this prependVector for consistency, on the model of the existing prependLazyList and prependChain?
There was a problem hiding this comment.
Sounds good. Happy to rename it so it's consistent with similar constructs. 👍
- Use idiomatic implementation - Use more consistent naming
|
Only a sightly related note do you know why the internal implementation of and we have implementations like this: which are also unsafe? I presume clients of the API can only create an instance of this class through its typesafe companion functions. But this would still open up the possibility to create a
I could ask this question on Gitter etc if that is a more appropriate place to ask this. Thanks! |
|
Closing and re-opening to trigger build |
Codecov Report
@@ Coverage Diff @@
## master #3362 +/- ##
=======================================
Coverage 93.34% 93.34%
=======================================
Files 378 378
Lines 7692 7693 +1
Branches 206 204 -2
=======================================
+ Hits 7180 7181 +1
Misses 512 512
Continue to review full report at Codecov.
|
|
@ssanj There's a lot of unmotivated inconsistency in the non-empty collections implementations, but in this case the difference is intentional. We want conversions between the standard library collection types and their non-empty It's both safe and efficient to check that a |
|
Thanks for the explanation @travisbrown 👍 |
I needed to implement this recently and was surprised it didn't already exist.
Thank you for contributing to Cats!
This is a kind reminder to run
sbt +prePRand commit the changed files, if any, before submitting.