如何使用“Seq”字段修改此嵌套案例类?

时间:2015-10-21 13:54:48

标签: scala case-class lens

某些嵌套案例类和字段addressesSeq[Address]

// ... means other fields
case class Street(name: String, ...)
case class Address(street: Street, ...)
case class Company(addresses: Seq[Address], ...)
case class Employee(company: Company, ...)

我有一名员工:

val employee = Employee(Company(Seq(
    Address(Street("aaa street")),
    Address(Street("bbb street")),
    Address(Street("bpp street")))))

它有3个地址。

我想把街道的首都用“b”开头。我的代码很乱如下:

val modified = employee.copy(company = employee.company.copy(addresses = 
    employee.company.addresses.map { address =>
        address.copy(street = address.street.copy(name = {
          if (address.street.name.startsWith("b")) {
            address.street.name.capitalize
          } else {
            address.street.name
          }
        }))
      }))

modified员工是:

Employee(Company(List(
    Address(Street(aaa street)), 
    Address(Street(Bbb street)), 
    Address(Street(Bpp street)))))

我正在寻找一种方法来改进它,但找不到一种方法。即使尝试了Monocle,也无法将其应用于此问题。

有没有办法让它变得更好?

PS:有两个关键要求:

  1. 仅使用不可变数据
  2. 不要丢失其他现有字段

3 个答案:

答案 0 :(得分:14)

正如Peter Neyens指出的那样,Shapeless的SYB在这里工作得非常好,但它会修改树中的所有 Street值,这可能并不总是你想要的。如果您需要更多控制路径,Monocle可以提供帮助:

import monocle.Traversal
import monocle.function.all._, monocle.macros._, monocle.std.list._

val employeeStreetNameLens: Traversal[Employee, String] =
  GenLens[Employee](_.company).composeTraversal(
    GenLens[Company](_.addresses)
      .composeTraversal(each)
      .composeLens(GenLens[Address](_.street))
      .composeLens(GenLens[Street](_.name))
  )

  val capitalizer = employeeStreeNameLens.modify {
    case s if s.startsWith("b") => s.capitalize
    case s => s
  }

正如Julien Truffaut在编辑中指出的那样,你可以通过创建一个镜头一直到街道名称的第一个角色来使这个更简洁(但不那么通用):

import monocle.std.string._

val employeeStreetNameFirstLens: Traversal[Employee, Char] =
  GenLens[Employee](_.company.addresses)
    .composeTraversal(each)
    .composeLens(GenLens[Address](_.street.name))
    .composeOptional(headOption)

val capitalizer = employeeStreetNameFirstLens.modify {
  case 'b' => 'B'
  case s   => s
}

有一些符号运算符会使上面的定义更加简洁,但我更喜欢非符号版本。

然后(为了清楚起见重新格式化结果):

scala> capitalizer(employee)
res3: Employee = Employee(
  Company(
    List(
      Address(Street(aaa street)),
      Address(Street(Bbb street)),
      Address(Street(Bpp street))
    )
  )
)

请注意,与在Shapeless答案中一样,您需要将Employee定义更改为使用List而不是Seq,或者如果您不想更改模型,您可以使用Lens将该转换构建到Iso[Seq[A], List[A]]

答案 1 :(得分:8)

如果您愿意将addresses中的CompanySeq替换为List,则可以使用无形的“废弃锅炉”(example )。

import shapeless._, poly._

case class Street(name: String)
case class Address(street: Street)
case class Company(addresses: List[Address])
case class Employee(company: Company)

val employee = Employee(Company(List(
    Address(Street("aaa street")),
    Address(Street("bbb street")),
    Address(Street("bpp street")))))

如果名称以“b”开头,您可以创建一个多态函数,该函数将Street的名称大写。

object capitalizeStreet extends ->(
  (s: Street) => {
    val name = if (s.name.startsWith("b")) s.name.capitalize else s.name
    Street(name)
  }
)

您可以将其用作:

val afterCapitalize = everywhere(capitalizeStreet)(employee)
// Employee(Company(List(
//   Address(Street(aaa street)), 
//   Address(Street(Bbb street)), 
//   Address(Street(Bpp street)))))

答案 2 :(得分:2)

  

查看quicklens

你可以这样做

import com.softwaremill.quicklens._

case class Street(name: String)
case class Address(street: Street)
case class Company(address: Seq[Address])
case class Employee(company: Company)
object Foo {
  def foo(e: Employee) = {
    modify(e)(_.company.address.each.street.name).using {
      case name if name.startsWith("b") => name.capitalize
      case name => name
    }
  }
}