implement enum show

This commit is contained in:
Mark Canlas 2020-08-14 23:26:49 -04:00
parent 8794f2ddea
commit 6924f6c47f
2 changed files with 15 additions and 20 deletions

View File

@ -1,14 +1,12 @@
package com.htmlism.mos6502.dsl
import cats.implicits._
sealed trait Color
object Color {
implicit val ev: Operand[Color] =
Operand
.operandInt
.contramap(toByte)
.contra(toByte, _.toString)
def toByte(x: Color): Int =
x match {

View File

@ -1,13 +1,25 @@
package com.htmlism.mos6502.dsl
import cats.Contravariant
trait Operand[A] {
self =>
def toAddressLiteral(x: A): String
def toShow(x: A): String
def operandType: OperandType
def contra[B](f: B => A, show: B => String): Operand[B] =
new Operand[B] {
val operandType: OperandType =
self.operandType
def toShow(x: B): String =
show(x)
def toAddressLiteral(x: B): String =
self.toAddressLiteral(f(x))
}
}
object Operand {
@ -22,19 +34,4 @@ object Operand {
def toAddressLiteral(x: Int): String =
String.format("#$%02x", x)
}
implicit val contra: Contravariant[Operand] =
new Contravariant[Operand] {
def contramap[A, B](fa: Operand[A])(f: B => A): Operand[B] =
new Operand[B] {
val operandType: OperandType =
fa.operandType
def toShow(x: B): String =
fa.toShow(f(x))
def toAddressLiteral(x: B): String =
fa.toAddressLiteral(f(x))
}
}
}