-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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 size comparison methods to IterableOps #6950
Conversation
* is `O(size min _size)` instead of `O(size)`. The method should be overwritten | ||
* if computing `size` is cheap. | ||
*/ | ||
def sizeCompare(_size: Int): Int = { |
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 super happy with this parameter name, but I wasn't sure what else to call it, as shadowing the size
method would be confusing
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.
Maybe otherSize
or thatSize
?
* this.sizeIs > size // this.sizeCompare(size) > 0 | ||
* }}} | ||
*/ | ||
@inline final def sizeIs: IterableOps.SizeCompareOps = new IterableOps.SizeCompareOps(this) |
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 sure if lengthIs
should just alias to this, or what. The issue is, then the documentation for all of its ops would say "size" instead of "length"; I'm not sure if that's an issue.
As it is right now, IterableOps.SizeCompareOps
and SeqOps.LengthCompareOps
are pretty much the same
Yes, this needs at least a test for |
If the results of |
@dwijnand because Java (probably) :(
Early on, someone decided that I am 100% with you, especially since I'm pretty sure returning an |
Let's break some eggs 😄. There is the
Can't the Java user
|
oh, I just meant because Java decided to return an |
We don't have zero-cost abstraction of enums that work in a Scala-friendly way, so I don't think there are good options for returning a trinary that won't slow things down. Since the point of these methods is for speed, typically, slowing things down seems like a bad idea. (Point is--ints are really fast. If you do anything else, it should be accompanied by a benchmark indicating that the difference isn't an issue. Even if it were a good idea to break with precedent, which it probably isn't.) |
agree |
Is int |
Note: my feedback shouldn't in any way be considered a blocker to this change landing. AFAIC this can land as is. |
@dwijnand - It's a smaller factor than I thought. If we have
then in a tight loop I measure it to be less than 5% slower than using |
That's interesting! Thanks for sharing. |
* is `O(size min _size)` instead of `O(size)`. The method should be overwritten | ||
* if computing `size` is cheap. | ||
*/ | ||
def sizeCompare(_size: Int): Int = { |
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.
Maybe otherSize
or thatSize
?
* is `O(this.size min that.size)` instead of `O(this.size + that.size)`. | ||
* The method should be overwritten if computing `size` is cheap. | ||
*/ | ||
def compareSizesWith(that: Iterable[_]): Int = { |
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.
What about using the singular for “size” here: compareSizeWith
?
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.
IANAG(rammarian), but I thought that "sizes" makes more sense, since I am comparing the size of this to the size of that (i.e. two sizes), rather than the size of this to that (which would be one size, and also nonsense). I am happy to be be corrected by someone that plural is not more grammatically correct in this case.
Edit: e.g. "I compared phones with my friend"
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.
If most others thinks compareSizeWith
is more natural or intuitive, I'm happy to change it
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.
Do you feel more comfortable with compareBySizeWith
?
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.
Neither of the With
ones are great because we normally use With
to mean we're using a different comparator (e.g. sortWith
). compareSizeTo
is fine (shorthand for "compare my size to that
's size"). Or, as we did with compare
, we could simply drop the To
--not compareTo
, just compare
, so not compareSizeTo
but just compareSize
.
The plural works too (shorthand for "compare our respective sizes"), but since the non-plural version has a sensible meaning I'd go for it instead. Having to remember random s's is annoying.
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.
honestly, that makes me more inclined to call it sizeCompare
. I think it makes more sense grammatically than compareSize
(which I still opine ought to use the plural), and is also consistent with the other sizeCompare
method. I think that compareSizesWith
is sufficiently different from sizeCompare
that one would be unlikely to mix them up, but having both sizeCompare
and compareSize
would quite confusing.
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.
Overloading sizeCompare
looks good to me.
val seq = Seq(1, 2, 3) | ||
assert(seq.compareSizesWith(Seq(1, 2)) > 0) | ||
assert(seq.compareSizesWith(Seq(1, 2, 3)) == 0) | ||
assert(seq.compareSizesWith(Seq(1, 2, 3, 4)) < 0) |
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 we should explicitly use List
here, and then write another test with a collection implementation that have a knownSize
different from -1.
Add IterableOps.sizeCompare(Int), .sizeCompare(Iterable[_]), and .sizeIs
4323252
to
68161af
Compare
ping |
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 Nth!
Add
IterableOps.sizeCompare(Int)
,.sizeCompare(Iterable[_])
, and.sizeIs
Addresses this comment on scala/collection-strawman#338