问题 如何在Scala中表示对case classe的部分更新?


我有以下案例类:

case class PropositionContent(title:String,content:String)

我想代表它作为数据的部分修改。

一种方法是创建案例类:

case class PartialPropositionContent(title:Option[String],content:Option[String)

然后是一些方法:

object PropositionContent {

   def append( pc  : PropositionContent
             , ppc : PartialPropositionContent) =
   PropositionContent ( ppc.title.getOrElse(pc.title)
                      , ppc.content.getOrElse(pc.content) )

   def append( ppc  : PartialPropositionContent
             , ppc2 : PartialPropositionContent ):  PartialPropositionContent = {...}

}

但这有点像锅炉架!

我想一个 case class PropositionContent[M[_]](title:M[String],content:M[String]) 不会真正解决这些问题,我不知道如何使用Shapeless解决问题。

你有什么想法吗?


2331
2018-04-27 09:04


起源

暗示: blog.stackmob.com/2012/02/an-introduction-to-lenses-in-scalaz - Alois Cochard
类型别名怎么样? - kiritsuku
你的意思是 type CompleteProposistionContent = PropositionContent[Id]? - jwinandy
@Alois Cochard我知道镜头,但是我需要对Data进行部分修改,我可以通过线路发送ser / der,它具有结构上的相同性。 - jwinandy


答案:


这是一个使用Shapeless的相对无样板的方法。首先,我们定义相关函数的一些多态版本 Option

import shapeless._

object orElser extends Poly1 {
  implicit def default[A] = at[Option[A]] {
    oa => (o: Option[A]) => oa orElse o
  }
}

object getOrElser extends Poly1 {
  implicit def default[A] = at[Option[A]] {
    oa => (a: A) => oa getOrElse a
  }
}

我们将代表更新 HList 每个元素都是一个 Option,我们可以写一个 append 允许我们附加两个更新的方法:

import UnaryTCConstraint._

def append[U <: HList: *->*[Option]#λ, F <: HList](u: U, v: U)(implicit
  mapper: MapperAux[orElser.type, U, F],
  zipper: ZipApplyAux[F, U, U]
): U = v.map(orElser).zipApply(u)

最后我们可以写我们的 update 方法本身:

def update[T, L <: HList, F <: HList, U <: HList](t: T, u: U)(implicit
  iso: Iso[T, L],
  mapped: MappedAux[L, Option, U],
  mapper: MapperAux[getOrElser.type, U, F],
  zipper: ZipApplyAux[F, L, L]
) = iso from u.map(getOrElser).zipApply(iso to t)

现在我们只需要一些样板(在 未来 这是没有必要的,谢谢 推理驱动宏):

implicit def pcIso =
  Iso.hlist(PropositionContent.apply _, PropositionContent.unapply _)

我们还将为此特定更新类型定义别名(这不是绝对必要的,但会使以下示例更简洁):

type PCUpdate = Option[String] :: Option[String] :: HNil

最后:

scala> val pc = PropositionContent("some title", "some content")
pc: PropositionContent = PropositionContent(some title,some content)

scala> val u1: PCUpdate = Some("another title") :: None :: HNil
u1: PCUpdate = Some(another title) :: None :: HNil

scala> val u2: PCUpdate = Some("newest title") :: Some("new content") :: HNil
u2: PCUpdate = Some(newest title) :: Some(new content) :: HNil

scala> append(u1, u2)
res0: PCUpdate = Some(newest title) :: Some(new content) :: HNil

scala> update(pc, append(u1, u2))
res1: PropositionContent = PropositionContent(newest title,new content)

这就是我们想要的。


8
2018-04-27 12:49





刚将Travis的代码(我不明白)移植到Shapeless 2.1:

import shapeless._
import shapeless.ops.hlist._

object orElser extends Poly1 {
  implicit def default[A]: Case[Option[A]] { type Result = Option[A] => Option[A] } = at[Option[A]] {
    oa => (o: Option[A]) => oa orElse o
  }
}

object getOrElser extends Poly1 {
  implicit def default[A]: Case[Option[A]] { type Result = A => A } = at[Option[A]] {
    oa => (a: A) => oa getOrElse a
  }
}

import UnaryTCConstraint._

def append[U <: HList: *->*[Option]#λ, F <: HList](u: U, v: U)
                                                  (implicit mapper: Mapper.Aux[orElser.type, U, F],
                                                            zipper: ZipApply.Aux[F, U, U]): U =
  v map orElser zipApply u

def update[T, L <: HList, F <: HList, U <: HList](t: T, u: U)
                                                 (implicit gen: Generic.Aux[T, L],
                                                           mapped: Mapped.Aux[L, Option, U],
                                                           mapper: Mapper.Aux[getOrElser.type, U, F],
                                                           zipper: ZipApply.Aux[F, L, L]) =
  gen from (u map getOrElser zipApply (gen to t))

API完好无损。


3
2018-02-28 13:57



(我不明白)  =>似乎是很多无形代码的情况:) - eirirlar