From 6924f6c47ff3d5e2b87d149e38d8f3003be3cbb7 Mon Sep 17 00:00:00 2001 From: Mark Canlas Date: Fri, 14 Aug 2020 23:26:49 -0400 Subject: [PATCH] implement enum show --- .../scala/com/htmlism/mos6502/dsl/Color.scala | 4 +-- .../com/htmlism/mos6502/dsl/Operand.scala | 31 +++++++++---------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Color.scala b/src/main/scala/com/htmlism/mos6502/dsl/Color.scala index 79d7b72..fadc8b0 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Color.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Color.scala @@ -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 { diff --git a/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala b/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala index a505057..6cda8c7 100644 --- a/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala +++ b/src/main/scala/com/htmlism/mos6502/dsl/Operand.scala @@ -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)) - } - } }