-
-
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
Fix many cats-kernel instances. #1324
Changes from 2 commits
706effc
aeee001
e2e0996
cffe60f
db2eab3
2d3b474
24e4de2
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 |
---|---|---|
@@ -1,7 +1,7 @@ | ||
package cats | ||
package instances | ||
|
||
trait BigDecimalInstances { | ||
trait BigDecimalInstances extends cats.kernel.instances.BigDecimalInstances { | ||
implicit val catsStdShowForBigDecimal: Show[BigDecimal] = | ||
Show.fromToString[BigDecimal] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,7 @@ trait ListInstances extends cats.kernel.instances.ListInstances { | |
|
||
implicit def catsStdShowForList[A:Show]: Show[List[A]] = | ||
new Show[List[A]] { | ||
def show(fa: List[A]): String = fa.map(_.show).mkString("List(", ", ", ")") | ||
def show(fa: List[A]): String = | ||
fa.iterator.map(_.show).mkString("List(", ", ", ")") | ||
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. nice catch |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,11 @@ import scala.annotation.tailrec | |
trait MapInstances extends cats.kernel.instances.MapInstances { | ||
|
||
implicit def catsStdShowForMap[A, B](implicit showA: Show[A], showB: Show[B]): Show[Map[A, B]] = | ||
Show.show[Map[A, B]] { m => | ||
val body = m.map { case (a, b) => | ||
s"${showA.show(a)} -> ${showB.show(b)})" | ||
}.mkString(",") | ||
s"Map($body)" | ||
new Show[Map[A, B]] { | ||
def show(m: Map[A, B]): String = | ||
m.iterator | ||
.map { case (a, b) => showA.show(a) + " -> " + showB.show(b) } | ||
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. why not 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 thought the compiler would rewrite it to us a string builder, but maybe that's only Java? I can rewrite this using interpolation (it just ends up looking uglier IMO). |
||
.mkString("Map(", ", ", ")") | ||
} | ||
|
||
// scalastyle:off method.length | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package cats.kernel | ||
package instances | ||
|
||
import scala.collection.immutable.BitSet | ||
|
||
package object bitSet extends BitSetInstances | ||
|
||
trait BitSetInstances { | ||
implicit val catsKernelStdPartialOrderForBitSet: PartialOrder[BitSet] = | ||
new BitSetPartialOrder | ||
|
||
implicit val catsKernelStdSemilatticeForBitSet: BoundedSemilattice[BitSet] = | ||
new BitSetSemilattice | ||
} | ||
|
||
class BitSetPartialOrder extends PartialOrder[BitSet] { | ||
def partialCompare(x: BitSet, y: BitSet): Double = | ||
if (x eq y) 0.0 | ||
else if (x.size < y.size) if (x.subsetOf(y)) -1.0 else Double.NaN | ||
else if (y.size < x.size) if (y.subsetOf(x)) 1.0 else Double.NaN | ||
else if (x == y) 0.0 | ||
else Double.NaN | ||
|
||
override def eqv(x: BitSet, y: BitSet): Boolean = | ||
x == y | ||
} | ||
|
||
class BitSetSemilattice extends BoundedSemilattice[BitSet] { | ||
def empty: BitSet = BitSet.empty | ||
def combine(x: BitSet, y: BitSet): BitSet = x | y | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cats.kernel | ||
package instances | ||
|
||
package object either extends EitherInstances | ||
|
||
trait EitherInstances extends EitherInstances0 { | ||
|
||
implicit def catsStdOrderForEither[A, B](implicit A: Order[A], B: Order[B]): Order[Either[A, B]] = | ||
new Order[Either[A, B]] { | ||
def compare(x: Either[A, B], y: Either[A, B]): Int = | ||
x match { | ||
case Left(xx) => y match { | ||
case Left(yy) => A.compare(xx, yy) | ||
case Right(_) => -1 | ||
} | ||
case Right(xx) => y match { | ||
case Left(_) => 1 | ||
case Right(yy) => B.compare(xx, yy) | ||
} | ||
} | ||
} | ||
|
||
implicit def catsDataMonoidForEither[A, B](implicit B: Monoid[B]): Monoid[Either[A, B]] = | ||
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. still feel #1321 is an issue. 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 thought of that but I didn't want to make this PR big and contentious. 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. no semigroup here? I think we can have with |
||
new Monoid[Either[A, B]] { | ||
def empty: Either[A, B] = | ||
Right(B.empty) | ||
def combine(x: Either[A, B], y: Either[A, B]): Either[A, B] = | ||
x match { | ||
case left @ Left(_) => left | ||
case Right(xx) => y match { | ||
case left @ Left(_) => left | ||
case Right(yy) => Right(B.combine(xx, yy)) | ||
} | ||
} | ||
} | ||
} | ||
|
||
trait EitherInstances0 extends EitherInstances1 { | ||
|
||
implicit def catsStdPartialOrderForEither[A, B](implicit A: PartialOrder[A], B: PartialOrder[B]): PartialOrder[Either[A, B]] = | ||
new PartialOrder[Either[A, B]] { | ||
def partialCompare(x: Either[A, B], y: Either[A, B]): Double = | ||
x match { | ||
case Left(xx) => y match { | ||
case Left(yy) => A.partialCompare(xx, yy) | ||
case Right(_) => -1.0 | ||
} | ||
case Right(xx) => y match { | ||
case Left(_) => 1.0 | ||
case Right(yy) => B.partialCompare(xx, yy) | ||
} | ||
} | ||
} | ||
} | ||
|
||
trait EitherInstances1 { | ||
implicit def catsStdEqForEither[A, B](implicit A: Eq[A], B: Eq[B]): Eq[Either[A, B]] = | ||
new Eq[Either[A, B]] { | ||
def eqv(x: Either[A, B], y: Either[A, B]): Boolean = | ||
x match { | ||
case Left(xx) => y match { | ||
case Left(yy) => A.eqv(xx, yy) | ||
case Right(_) => false | ||
} | ||
case Right(xx) => y match { | ||
case Left(_) => false | ||
case Right(yy) => B.eqv(xx, yy) | ||
} | ||
} | ||
} | ||
} |
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.
we have biased to
match
for alleged performance reasons. What do you think of it here?