Skip to content

Commit

Permalink
Convert Band and Semilattice instances to SAM syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
joroKr21 committed Aug 12, 2022
1 parent bf8ee7f commit 132b4c3
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ trait BoundedJoinSemilattice[@sp(Int, Long, Float, Double) A] extends Any with J
def isZero(a: A)(implicit ev: Eq[A]): Boolean = ev.eqv(a, zero)

override def joinSemilattice: BoundedSemilattice[A] =
BoundedSemilattice.instance(zero, join)
new BoundedSemilattice[A] {
def empty: A = zero
def combine(x: A, y: A): A = join(x, y)
}
}

trait BoundedJoinSemilatticeFunctions[B[A] <: BoundedJoinSemilattice[A]] extends JoinSemilatticeFunctions[B] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ trait BoundedMeetSemilattice[@sp(Int, Long, Float, Double) A] extends Any with M
def isOne(a: A)(implicit ev: Eq[A]): Boolean = ev.eqv(a, one)

override def meetSemilattice: BoundedSemilattice[A] =
BoundedSemilattice.instance(one, meet)
new BoundedSemilattice[A] {
def empty: A = one
def combine(x: A, y: A): A = meet(x, y)
}
}

trait BoundedMeetSemilatticeFunctions[B[A] <: BoundedMeetSemilattice[A]] extends MeetSemilatticeFunctions[B] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import scala.{specialized => sp}
trait JoinSemilattice[@sp(Int, Long, Float, Double) A] extends Any with Serializable {
def join(lhs: A, rhs: A): A

def joinSemilattice: Semilattice[A] =
Semilattice.instance(join)
def joinSemilattice: Semilattice[A] = join(_, _)

def joinPartialOrder(implicit ev: Eq[A]): PartialOrder[A] =
joinSemilattice.asJoinPartialOrder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ import scala.{specialized => sp}
trait MeetSemilattice[@sp(Int, Long, Float, Double) A] extends Any with Serializable {
def meet(lhs: A, rhs: A): A

def meetSemilattice: Semilattice[A] =
Semilattice.instance(meet)
def meetSemilattice: Semilattice[A] = meet(_, _)

def meetPartialOrder(implicit ev: Eq[A]): PartialOrder[A] =
meetSemilattice.asMeetPartialOrder
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/scala/cats/data/NonEmptyMapImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,7 @@ sealed abstract private[data] class NonEmptyMapInstances extends NonEmptyMapInst
implicit def catsDataShowForNonEmptyMap[K: Show, A: Show]: Show[NonEmptyMap[K, A]] = _.show

@deprecated("Use catsDataSemigroupForNonEmptyMap", "2.5.0")
def catsDataBandForNonEmptyMap[K, A]: Band[NonEmptyMap[K, A]] =
Band.instance(_ ++ _)
def catsDataBandForNonEmptyMap[K, A]: Band[NonEmptyMap[K, A]] = _ ++ _

implicit def catsDataSemigroupForNonEmptyMap[K, A: Semigroup]: Semigroup[NonEmptyMap[K, A]] =
(x, y) => NonEmptyMap.fromMapUnsafe(Semigroup[SortedMap[K, A]].combine(x.toSortedMap, y.toSortedMap))
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/scala/cats/data/NonEmptySet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,9 @@ sealed abstract private[data] class NonEmptySetInstances extends NonEmptySetInst
implicit def catsDataShowForNonEmptySet[A: Show]: Show[NonEmptySet[A]] = _.show

implicit def catsDataSemilatticeForNonEmptySet[A]: Semilattice[NonEmptySet[A]] =
Semilattice.instance(_ | _)
new Semilattice[NonEmptySet[A]] {
def combine(x: NonEmptySet[A], y: NonEmptySet[A]): NonEmptySet[A] = x | y
}
}

sealed abstract private[data] class NonEmptySetInstances0 extends NonEmptySetInstances1 {
Expand Down
3 changes: 1 addition & 2 deletions kernel/src/main/scala/cats/kernel/Band.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@ object Band extends SemigroupFunctions[Band] {
/**
* Create a `Band` instance from the given function.
*/
@inline def instance[@sp(Int, Long, Float, Double) A](cmb: (A, A) => A): Band[A] =
cmb(_, _)
@inline def instance[A](cmb: (A, A) => A): Band[A] = cmb(_, _)
}
2 changes: 1 addition & 1 deletion kernel/src/main/scala/cats/kernel/BoundedSemilattice.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ object BoundedSemilattice extends SemilatticeFunctions[BoundedSemilattice] {
/**
* Create a `BoundedSemilattice` instance from the given function and empty value.
*/
@inline def instance[@sp(Int, Long, Float, Double) A](emptyValue: A, cmb: (A, A) => A): BoundedSemilattice[A] =
@inline def instance[A](emptyValue: A, cmb: (A, A) => A): BoundedSemilattice[A] =
new BoundedSemilattice[A] {
override val empty: A = emptyValue
override def combine(x: A, y: A): A = cmb(x, y)
Expand Down
3 changes: 1 addition & 2 deletions kernel/src/main/scala/cats/kernel/Semilattice.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,5 @@ object Semilattice extends SemilatticeFunctions[Semilattice] {
/**
* Create a `Semilattice` instance from the given function.
*/
@inline def instance[@sp(Int, Long, Float, Double) A](cmb: (A, A) => A): Semilattice[A] =
cmb(_, _)
@inline def instance[A](cmb: (A, A) => A): Semilattice[A] = cmb(_, _)
}
6 changes: 4 additions & 2 deletions laws/src/main/scala/cats/laws/discipline/MiniInt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ object MiniInt {
def combine(x: MiniInt, y: MiniInt): MiniInt = x * y
}

val miniIntOr: BoundedSemilattice[MiniInt] =
BoundedSemilattice.instance(MiniInt.zero, _ | _)
val miniIntOr: BoundedSemilattice[MiniInt] = new BoundedSemilattice[MiniInt] {
val empty = MiniInt.zero
def combine(x: MiniInt, y: MiniInt): MiniInt = x | y
}
}

0 comments on commit 132b4c3

Please sign in to comment.