-
-
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
Add missing Chain#distinctBy method #4156
Changes from all commits
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 |
---|---|---|
|
@@ -27,8 +27,8 @@ import cats.kernel.instances.StaticMethods | |
|
||
import scala.annotation.tailrec | ||
import scala.collection.immutable.SortedMap | ||
import scala.collection.immutable.TreeSet | ||
import scala.collection.immutable.{IndexedSeq => ImIndexedSeq} | ||
import scala.collection.mutable | ||
import scala.collection.mutable.ListBuffer | ||
|
||
import Chain.{ | ||
|
@@ -151,6 +151,10 @@ sealed abstract class Chain[+A] extends ChainCompat[A] { | |
*/ | ||
final def nonEmpty: Boolean = !isEmpty | ||
|
||
// Quick check whether the chain is either empty or contains one element only. | ||
@inline private def isEmptyOrSingleton: Boolean = | ||
isEmpty || this.isInstanceOf[Chain.Singleton[_]] | ||
|
||
/** | ||
* Concatenates this with `c` in O(1) runtime. | ||
*/ | ||
|
@@ -760,19 +764,59 @@ sealed abstract class Chain[+A] extends ChainCompat[A] { | |
|
||
/** | ||
* Remove duplicates. Duplicates are checked using `Order[_]` instance. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.Chain | ||
* scala> val chain = Chain(1, 2, 2, 3) | ||
* scala> chain.distinct | ||
* res0: cats.data.Chain[Int] = Chain(1, 2, 3) | ||
* }}} | ||
*/ | ||
def distinct[AA >: A](implicit O: Order[AA]): Chain[AA] = { | ||
implicit val ord: Ordering[AA] = O.toOrdering | ||
|
||
var alreadyIn = TreeSet.empty[AA] | ||
if (isEmptyOrSingleton) this | ||
else { | ||
implicit val ord: Ordering[AA] = O.toOrdering | ||
|
||
val bldr = Vector.newBuilder[AA] | ||
val seen = mutable.TreeSet.empty[AA] | ||
val it = iterator | ||
while (it.hasNext) { | ||
val next = it.next() | ||
if (seen.add(next)) | ||
bldr += next | ||
} | ||
// Result can contain a single element only. | ||
Chain.fromSeq(bldr.result()) | ||
} | ||
} | ||
|
||
foldLeft(Chain.empty[AA]) { (elementsSoFar, b) => | ||
if (alreadyIn.contains(b)) { | ||
elementsSoFar | ||
} else { | ||
alreadyIn += b | ||
elementsSoFar :+ b | ||
/** | ||
* Remove duplicates by a predicate. Duplicates are checked using `Order[_]` instance. | ||
* | ||
* Example: | ||
* {{{ | ||
* scala> import cats.data.Chain | ||
* scala> val chain = Chain(1, 2, 3, 4) | ||
* scala> chain.distinctBy(_ / 2) | ||
* res0: cats.data.Chain[Int] = Chain(1, 2, 4) | ||
* }}} | ||
*/ | ||
def distinctBy[B](f: A => B)(implicit O: Order[B]): Chain[A] = { | ||
if (isEmptyOrSingleton) this | ||
else { | ||
implicit val ord: Ordering[B] = O.toOrdering | ||
|
||
val bldr = Vector.newBuilder[A] | ||
val seen = mutable.TreeSet.empty[B] | ||
val it = iterator | ||
while (it.hasNext) { | ||
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. one issue: this is going to create a I wonder if the better approach would build a Vector and e.g. val bldr = Vector.newBuilder[A]
...
Wrap(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. Yeah, I wonder too. I didn't really think about it, perhaps you're right. Let's try this option. 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. fixed |
||
val next = it.next() | ||
if (seen.add(f(next))) | ||
bldr += next | ||
} | ||
// Result can contain a single element only. | ||
Chain.fromSeq(bldr.result()) | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/* | ||
* Copyright (c) 2015 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package cats.tests.compat | ||
|
||
import scala.collection.immutable.Seq | ||
|
||
private[tests] trait ScalaVersionSpecificSyntax { | ||
implicit final private[tests] def catsTestsCompatSeqOps[C[a] <: Seq[a], A](self: C[A]) = new SeqOps[C, A](self) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright (c) 2015 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package cats.tests.compat | ||
|
||
import scala.collection.generic.CanBuildFrom | ||
import scala.collection.immutable.Seq | ||
import scala.collection.mutable | ||
|
||
final private[tests] class SeqOps[C[a] <: Seq[a], A] private[compat] (private val self: C[A]) extends AnyVal { | ||
|
||
// Scala v2.12.x does not have `distinctBy` implemented. | ||
// Therefore this implementation is copied (and adapted) from Scala Library v2.13.8 sources: | ||
// https://github.com/scala/scala/blob/v2.13.8/src/library/scala/collection/immutable/StrictOptimizedSeqOps.scala#L26-L39 | ||
def distinctBy[B](f: A => B)(implicit cbf: CanBuildFrom[C[A], A, C[A]]): C[A] = { | ||
if (self.lengthCompare(1) <= 0) self | ||
else { | ||
val builder = cbf() | ||
val seen = mutable.HashSet.empty[B] | ||
val it = self.iterator | ||
var different = false | ||
while (it.hasNext) { | ||
val next = it.next() | ||
if (seen.add(f(next))) builder += next else different = true | ||
} | ||
if (different) builder.result() else self | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2015 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package cats.tests.compat | ||
|
||
private[tests] trait ScalaVersionSpecificSyntax {} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright (c) 2015 Typelevel | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
* this software and associated documentation files (the "Software"), to deal in | ||
* the Software without restriction, including without limitation the rights to | ||
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
* the Software, and to permit persons to whom the Software is furnished to do so, | ||
* subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in all | ||
* copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
package cats | ||
|
||
package object tests extends cats.tests.compat.ScalaVersionSpecificSyntax |
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.
Potentially this line (and the similar one below) could be expanded into something more efficient like
but... I'm not really sure it is worth it – I mean, it is unlikely that such a single call can lead to substantial (or even noticeable) performance decrease, IMO.