Content-Length: 351692 | pFad | http://github.com/scala/scala/pull/11047/files

D3 Add support for Iterable to sizeIs by wernerschram · Pull Request #11047 · scala/scala · GitHub
Skip to content

Add support for Iterable to sizeIs #11047

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

Open
wants to merge 1 commit into
base: 2.13.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 33 additions & 11 deletions src/library/scala/collection/Iterable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -286,18 +286,27 @@ trait IterableOps[+A, +CC[_], +C] extends Any with IterableOnce[A] with Iterable
}
}

/** Returns a value class containing operations for comparing the size of this $coll to a test value.
/** Returns a value class containing operations for comparing the size of this $coll to a test value or the size of
* another collection.
*
* These operations are implemented in terms of [[sizeCompare(Int) `sizeCompare(Int)`]], and
* allow the following more readable usages:
* These operations are implemented in terms of [[sizeCompare(Int) `sizeCompare(Int)`]] and
* [[sizeCompare(Iterable[_]) `sizeCompare(Iterable[_])`]]. They allow the following more readable usages:
*
* {{{
* this.sizeIs < size // this.sizeCompare(size) < 0
* this.sizeIs <= size // this.sizeCompare(size) <= 0
* this.sizeIs == size // this.sizeCompare(size) == 0
* this.sizeIs != size // this.sizeCompare(size) != 0
* this.sizeIs >= size // this.sizeCompare(size) >= 0
* this.sizeIs > size // this.sizeCompare(size) > 0
* this.sizeIs < size // this.sizeCompare(size) < 0
* this.sizeIs <= size // this.sizeCompare(size) <= 0
* this.sizeIs == size // this.sizeCompare(size) == 0
* this.sizeIs != size // this.sizeCompare(size) != 0
* this.sizeIs >= size // this.sizeCompare(size) >= 0
* this.sizeIs > size // this.sizeCompare(size) > 0
*
* this.sizeIs < that.sizeIs // this.sizeCompare(that) < 0
* this.sizeIs <= that.sizeIs // this.sizeCompare(that) <= 0
* this.sizeIs == that.sizeIs // this.sizeCompare(that) == 0
* this.sizeIs != that.sizeIs // this.sizeCompare(that) != 0
* this.sizeIs >= that.sizeIs // this.sizeCompare(that) >= 0
* this.sizeIs > that.sizeIs // this.sizeCompare(that) > 0
* this.sizeIs > that.sizeIs // this.sizeCompare(that) > 0
* }}}
*/
@inline final def sizeIs: IterableOps.SizeCompareOps = new IterableOps.SizeCompareOps(this)
Expand Down Expand Up @@ -860,10 +869,11 @@ trait IterableOps[+A, +CC[_], +C] extends Any with IterableOnce[A] with Iterable

object IterableOps {

/** Operations for comparing the size of a collection to a test value.
/** Operations for comparing the size of a collection to a test value or another collection.
*
* These operations are implemented in terms of
* [[scala.collection.IterableOps!.sizeCompare(Int):Int* `sizeCompare(Int)`]]
* [[scala.collection.IterableOps!.sizeCompare(Int):Int* `sizeCompare(Int)`]] and
* [[scala.collection.IterableOps!.sizeCompare(Iterable[_]):Int* `sizeCompare(Iterable[_])`]].
*/
final class SizeCompareOps private[collection](val it: IterableOps[_, AnyConstr, _]) extends AnyVal {
/** Tests if the size of the collection is less than some value. */
Expand All @@ -878,6 +888,18 @@ object IterableOps {
@inline def >=(size: Int): Boolean = it.sizeCompare(size) >= 0
/** Tests if the size of the collection is greater than some value. */
@inline def >(size: Int): Boolean = it.sizeCompare(size) > 0
/** Tests if the size of the collection is less than some other collection. */
@inline def <(other: SizeCompareOps): Boolean = it.sizeCompare(other.it.toIterable: @nowarn("cat=deprecation")) < 0
/** Tests if the size of the collection is less than or equal to some other collection. */
@inline def <=(other: SizeCompareOps): Boolean = it.sizeCompare(other.it.toIterable: @nowarn("cat=deprecation")) <= 0
/** Tests if the size of the collection is equal to some other collection. */
@inline def ==(other: SizeCompareOps): Boolean = it.sizeCompare(other.it.toIterable: @nowarn("cat=deprecation")) == 0
/** Tests if the size of the collection is not equal to some other collection. */
@inline def !=(other: SizeCompareOps): Boolean = it.sizeCompare(other.it.toIterable: @nowarn("cat=deprecation")) != 0
/** Tests if the size of the collection is greater than or equal to some other collection. */
@inline def >=(other: SizeCompareOps): Boolean = it.sizeCompare(other.it.toIterable: @nowarn("cat=deprecation")) >= 0
/** Tests if the size of the collection is greater than some other collection. */
@inline def >(other: SizeCompareOps): Boolean = it.sizeCompare(other.it.toIterable: @nowarn("cat=deprecation")) > 0
}

/** A trait that contains just the `map`, `flatMap`, `foreach` and `withFilter` methods
Expand Down
11 changes: 11 additions & 0 deletions test/junit/scala/collection/IterableTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ class IterableTest {
check(unknown, unknown)
}

@Test
def sizeIsIterable(): Unit = {
val it = Seq(1, 2, 3)
assert(it.sizeIs > Seq(2, 3).sizeIs)
assert(it.sizeIs != Seq(2, 3).sizeIs)
assert(it.sizeIs >= Seq(2, 3, 4).sizeIs)
assert(it.sizeIs == Seq(2, 3, 4).sizeIs)
assert(it.sizeIs <= Seq(2, 3, 4).sizeIs)
assert(it.sizeIs < Seq(2, 3, 4, 5).sizeIs)
}

@Test def copyToArray(): Unit = {
def check(a: Array[Int], copyToArray: Array[Int] => Int, elemsWritten: Int, start: Int, end: Int) = {

Expand Down
Loading








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/scala/scala/pull/11047/files

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy