scala - 在Scala中的案例类上应用递归操作
问题描述
我正在尝试找到一种优雅的方式来执行以下操作。
假设我们有以下类:
case class Foo(fooName: String, bar: Bar, baz: Baz)
case class Bar(barName: String, baz: Baz)
case class Baz(bazName: String)
我希望能够更改名称(在这种情况下以某种方式 Foo 及其子对象),例如,为它们加上一些标题。
我可能会定义一个用于该目的的类型类:
trait Titled[T] {
def titled(t: T)(title: String): T
}
对于 Foo,实现会这样做:
implicit val fooTitled: Titled[Foo] = new Titled[Foo] {
def titled(foo: Foo)(title: String): Foo = foo.copy(fooName = title + fooName)
}
可能还有其他实现,例如 Esquired,它会在名称后面加上“Esq”。
但这不会更新我想要的子值 bar 和 baz 的标题。
我最终想要的结果是这样的:
//import all the implicits needed
val foo = Foo(...)
val titledFoo = foo.title("Mr. ") // names are prefixed with Mr.
val andEsquired = foo.esquire // names are also suffixed with Esq
现在,我一直在网上寻找是否可以这样做,并且我已经看到了无形库,但我仍然没有达到我可以破译并创造这种“魔法”的 Scala 知识水平,如果它甚至可能的。
我不一定要寻找一个完整的解决方案(尽管我会很感激),但如果有人能指出我正确的方向,给我看一些例子、教程或类似的东西,我将非常感激。
解决方案
尝试以下类型类。我假设所有应该具有类型类实例的案例类都具有String
要作为前缀的类型的第一个字段。
import shapeless.ops.hlist.IsHCons
import shapeless.{::, Generic, HList, HNil}
trait Titled[T] {
def titled(t: T)(title: String): T
}
object Titled {
//implicit def identity[A]: Titled[A] = new Titled[A] {
// override def titled(t: A)(title: String): A = t
//}
implicit def transform[A <: Product, L <: HList, H, T <: HList](implicit
generic: Generic.Aux[A, L],
isHCons: IsHCons.Aux[L, String, T],
tailTitled: HListTitled[T]): Titled[A] = new Titled[A] {
override def titled(t: A)(title: String): A = {
val l = generic.to(t)
val head = isHCons.head(l)
val tail = isHCons.tail(l)
val newHead = title + head
val newTail = tailTitled.titled(tail)(title)
val newL = isHCons.cons(newHead, newTail)
generic.from(newL)
}
}
}
trait HListTitled[L <: HList] {
def titled(t: L)(title: String): L
}
object HListTitled {
implicit val hnil: HListTitled[HNil] = new HListTitled[HNil] {
override def titled(t: HNil)(title: String): HNil = HNil
}
implicit def hcons[H, T <: HList](implicit
headTitled: Titled[H],
tailTitled: HListTitled[T]): HListTitled[H :: T] = new HListTitled[H :: T] {
override def titled(t: H :: T)(title: String): H :: T =
headTitled.titled(t.head)(title) :: tailTitled.titled(t.tail)(title)
}
}
implicit class TitledOps[T](t: T) {
def titled(title: String)(implicit ttld: Titled[T]): T = ttld.titled(t)(title)
}
case class Foo(fooName: String, bar: Bar, baz: Baz)
case class Bar(barName: String, baz: Baz)
case class Baz(bazName: String)
case class Foo1(fooName: String, bar: Bar, baz: Baz, i: Int)
Foo("Johnson", Bar("Smith", Baz("Doe")), Baz("James")).titled("Mr. ")
// Foo(Mr. Johnson,Bar(Mr. Smith,Baz(Mr. Doe)),Baz(Mr. James))
Foo1("Johnson", Bar("Smith", Baz("Doe")), Baz("James"), 10).titled("Mr. ")
// doesn't compile
// if you want it to compile uncomment "identity"
推荐阅读
- android - 如何使用位图将检索到的图像从 Sqlite 数据库共享到社交媒体?
- java - 在spring data jpa中将postgres查询转换为jpql后没有结果
- ios - 仅使用 shell,我如何知道配置文件是 app-store 还是 ad-hoc?
- ionic-framework - 在 Ionic Windows 平台中将光标定位到字符串的末尾
- c - 联锁操作和与 _aligned_malloc 对齐
- c++ - What is the semantics of testb $1, %al
- php - 如何对评论发表评论
- javascript - vue-draggable - 不能直接改变我的数组
- uwp - 如何从 ItemTemplate UWP 中的 DataContext 获取命令
- java - SQLite 数据库 - 更新 RecyclerView 中的一行