Skip to content

Add TraversableOnce.knownSize #143

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ class IteratorExtensionMethods[A](private val self: c.Iterator[A]) extends AnyVa

class TraversableOnceExtensionMethods[A](private val self: c.TraversableOnce[A]) extends AnyVal {
def iterator: Iterator[A] = self.toIterator
def knownSize: Int = if (self.hasDefiniteSize) self.size else -1
}

class TraversableExtensionMethods[A](private val self: c.Traversable[A]) extends AnyVal {
Expand Down
13 changes: 13 additions & 0 deletions compat/src/test/scala/test/scala/collection/ExtensionsTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package test.scala.collection

import org.junit.{Assert, Test}

import scala.collection.compat._

class ExtensionsTest {
@Test
def `TraversableOnce.knownSize`(): Unit = {
Assert.assertEquals(-1, Iterator(1).knownSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 2.13 the size of an IndexedSeq’s iterator is known: https://github.com/scala/scala/blob/2.13.x/src/library/scala/collection/IndexedSeqView.scala#L27

You can use Iterator.from(1) instead.

Assert.assertEquals(1, List(1).knownSize)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The size of LinearSeqs is not known in 2.13.

Actually I realize only now that hasDefiniteSize has nothing to do with the fact that the size of the collection is cheaply computable. It only tells us whether the collection is finite or infinite. Consequently, this is not the correct way to implement knownSize. I’m not sure there is a correct way.

}
}
4 changes: 2 additions & 2 deletions scalafix/rules/src/main/scala/fix/Roughly.scala
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ final case class Roughly(index: SemanticdbIndex, config: RoughlyConfig)
ctx.tree.collect {
case ap @ Term.ApplyInfix(_, mapValues(_), _, _) if strictMapValues =>
ctx.addLeft(ap, "(") +
ctx.addRight(ap, ").toMap")
ctx.addRight(ap, ").toMap")

case ap @ Term.Apply(Term.Select(_, mapValues(_)), List(_)) if strictMapValues =>
ctx.addRight(ap, ".toMap")

case ap @ Term.ApplyInfix(_, filterKeys(_), _, _) if strictFilterKeys =>
ctx.addLeft(ap, "(") +
ctx.addRight(ap, ").toMap")
ctx.addRight(ap, ").toMap")

case ap @ Term.Apply(Term.Select(_, filterKeys(_)), List(_)) if strictFilterKeys =>
ctx.addRight(ap, ".toMap")
Expand Down