Skip to content
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 Cogen instances for SortedSet and SortedMap #927

Merged
merged 1 commit into from
Dec 1, 2022
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ import org.scalacheck.Prop.forAll
import org.scalacheck.rng.Seed
import ScalaVersionSpecific._


import scala.util.Try
import scala.collection.immutable.SortedMap
import scala.collection.immutable.SortedSet
import scala.concurrent.duration.{Duration, FiniteDuration}
import scala.util.Try

import java.time._
import java.util.UUID
Expand Down Expand Up @@ -181,4 +182,6 @@ object CogenSpecification extends Properties("Cogen") {
include(cogenLaws[ZonedDateTime], "cogenZonedDateTime.")
include(cogenLaws[ZoneId], "cogenZoneId.")
include(cogenLaws[Period], "cogenPeriod.")
include(cogenLaws[SortedSet[Int]], "cogenSortedSet.")
include(cogenLaws[SortedMap[Int, Int]], "cogenSortedSet.")
}
16 changes: 14 additions & 2 deletions core/shared/src/main/scala/org/scalacheck/Cogen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import scala.concurrent.duration.{Duration, FiniteDuration}
import java.math.BigInteger
import java.util.UUID
import rng.Seed
import scala.collection.immutable.SortedMap
import scala.collection.immutable.SortedSet

sealed trait Cogen[T] extends Serializable {

Expand Down Expand Up @@ -124,10 +126,20 @@ object Cogen extends CogenArities with CogenLowPriority with CogenVersionSpecifi
Cogen.it(_.iterator)

implicit def cogenSet[A: Cogen: Ordering]: Cogen[Set[A]] =
Cogen.it(_.toVector.sorted.iterator)
Cogen[SortedSet[A]].contramap(value =>
value.foldLeft(SortedSet.empty[A])(_ + _)
)

implicit def cogenSortedSet[A: Cogen]: Cogen[SortedSet[A]] =
Cogen.it(_.iterator)

implicit def cogenMap[K: Cogen: Ordering, V: Cogen]: Cogen[Map[K, V]] =
Cogen.it(_.toVector.sortBy(_._1).iterator)
Cogen[SortedMap[K, V]].contramap(value =>
value.foldLeft(SortedMap.empty[K, V])(_ + _)
)

implicit def cogenSortedMap[K: Cogen, V: Cogen]: Cogen[SortedMap[K, V]] =
Cogen.it(_.iterator)

implicit def cogenFunction0[Z: Cogen]: Cogen[() => Z] =
Cogen[Z].contramap(f => f())
Expand Down