-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Optimize distinctBy implementation for non-empty collections #4711
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -345,16 +345,15 @@ final case class NonEmptyList[+A](head: A, tail: List[A]) extends NonEmptyCollec | |
override def distinctBy[B](f: A => B)(implicit O: Order[B]): NonEmptyList[A] = { | ||
implicit val ord: Ordering[B] = O.toOrdering | ||
|
||
val buf = ListBuffer.empty[A] | ||
tail.foldLeft(TreeSet(f(head): B)) { (elementsSoFar, a) => | ||
val b = f(a) | ||
if (elementsSoFar(b)) elementsSoFar | ||
else { | ||
buf += a; elementsSoFar + b | ||
} | ||
val bldr = List.newBuilder[A] | ||
val seen = mutable.TreeSet.empty[B] | ||
val it = iterator | ||
while (it.hasNext) { | ||
val next = it.next() | ||
if (seen.add(f(next))) | ||
bldr += next | ||
} | ||
|
||
NonEmptyList(head, buf.toList) | ||
NonEmptyList.fromListUnsafe(bldr.result()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be slightly better to do: var rest = tail
seen.add(f(head))
while (rest.nonEmpty) {
val next = rest.head
rest = rest.tail
if (seen.add(f(next)) {
bldr += next
}
}
NonEmptyList(head, bldr.result()) the main changes:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd also suggest to check if the source NeList has 1 item only ( This small optimization could also be applied to the other two collections. |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -241,16 +241,15 @@ final class NonEmptySeq[+A] private (val toSeq: Seq[A]) extends AnyVal with NonE | |
override def distinctBy[B](f: A => B)(implicit O: Order[B]): NonEmptySeq[A] = { | ||
implicit val ord: Ordering[B] = O.toOrdering | ||
|
||
val buf = Seq.newBuilder[A] | ||
tail.foldLeft(TreeSet(f(head): B)) { (elementsSoFar, a) => | ||
val b = f(a) | ||
if (elementsSoFar(b)) elementsSoFar | ||
else { | ||
buf += a; elementsSoFar + b | ||
} | ||
val bldr = Seq.newBuilder[A] | ||
val seen = mutable.TreeSet.empty[B] | ||
val it = iterator | ||
while (it.hasNext) { | ||
val next = it.next() | ||
if (seen.add(f(next))) | ||
bldr += next | ||
} | ||
|
||
NonEmptySeq(head, buf.result()) | ||
NonEmptySeq.fromSeqUnsafe(bldr.result()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this one is different from NonEmptyList, because it is a wrapper on the whole Seq, unlike NonEmptyList which is a class. Given that, this implementation here seems good to me. |
||
} | ||
|
||
/** | ||
|
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.
actually this could be moved inside the
else
branch to avoid it on singletons.