For specific tasks like this, it is most often easier to create a type class than to dabble with something like a map
with a Poly
.
We can represent an update on some T
as follows :
trait Update[T] {
def apply(base: T, update: T): T
}
Now we need to define some instances. How to update a List
and Option
and some instances to make it possible to derive an Update[Foo]
instance.
import shapeless._
object Update extends Update0 {
def apply[A](implicit update: Lazy[Update[A]]): Update[A] = update.value
implicit def optionUpdate[A]: Update[Option[A]] =
new Update[Option[A]] {
def apply(base: Option[A], update: Option[A]): Option[A] = update orElse base
}
implicit def listUpdate[A]: Update[List[A]] =
new Update[List[A]] {
def apply(base: List[A], update: List[A]): List[A] = base ++ update
}
implicit def hnilUpdate: Update[HNil] =
new Update[HNil] {
def apply(base: HNil, update: HNil): HNil = HNil
}
implicit def hconsUpdate[H, T <: HList](
implicit updateH: Update[H], updateT: Lazy[Update[T]]
): Update[H :: T] =
new Update[H :: T] {
def apply(base: H :: T, update: H :: T): H :: T =
updateH(base.head, update.head) :: updateT.value(base.tail, update.tail)
}
}
trait Update0 {
implicit def genericUpdate[A, G <: HList](
implicit gen: Generic.Aux[A, G], updateG: Lazy[Update[G]]
): Update[A] =
new Update[A] {
def apply(base: A, update: A): A =
gen.from(updateG.value(gen.to(base), gen.to(update)))
}
}
We can add some syntax to make it a little bit easier :
implicit class UpdateOps[A](val base: A) extends AnyVal {
def update(change: A)(implicit update: Lazy[Update[A]]): A =
update.value(base, change)
}
Now we can do :
case class Foo(a: Option[Int], b: List[Int], c: Option[Int])
val base = Foo(None, Nil, Some(0))
val update = Foo(Some(3), List(4), None)
base update update // Foo(Some(3),List(4),Some(0))
We could define an instance for cats.SemigroupK
or scalaz.Plus
so we could omit the Option
and List
instances, while gaining eg Update[Vector[Int]]
:
import cats.SemigroupK
import cats.implicits._
implicit def semigroupKUpdate[F[_], A](implicit F: SemigroupK[F]): Update[F[A]] =
new Update[F[A]] {
def apply(base: F[A], update: F[A]): F[A] = F.combineK(update, base)
}