8

下面的代码成功了,但是有没有更好的方法来做同样的事情?也许是特定于案例类的东西?在下面的代码中,对于我的简单案例类中的每个 String 类型的字段,代码会遍历该案例类的实例列表并找到该字段的最长字符串的长度。

case class CrmContractorRow(
                             id: Long,
                             bankCharges: String,
                             overTime: String,
                             name$id: Long,
                             mgmtFee: String,
                             contractDetails$id: Long,
                             email: String,
                             copyOfVisa: String)

object Go {
  def main(args: Array[String]) {
    val a = CrmContractorRow(1,"1","1",4444,"1",1,"1","1")
    val b = CrmContractorRow(22,"22","22",22,"55555",22,"nine long","22")
    val c = CrmContractorRow(333,"333","333",333,"333",333,"333","333")
    val rows = List(a,b,c)

    c.getClass.getDeclaredFields.filter(p => p.getType == classOf[String]).foreach{f =>
      f.setAccessible(true)
      println(f.getName + ": " + rows.map(row => f.get(row).asInstanceOf[String]).maxBy(_.length))
    }
  }
}

结果:

bankCharges: 3
overTime: 3
mgmtFee: 5
email: 9
copyOfVisa: 3
4

4 回答 4

11

如果你想用 Shapeless 做这种事情,我强烈建议定义一个自定义类型类来处理复杂的部分,并允许你将这些东西与你的逻辑的其余部分分开。

在这种情况下,听起来您具体尝试做的棘手部分是获取String案例类所有成员从字段名称到字符串长度的映射。这是一个执行此操作的类型类:

import shapeless._, shapeless.labelled.FieldType

trait StringFieldLengths[A] { def apply(a: A): Map[String, Int] }

object StringFieldLengths extends LowPriorityStringFieldLengths {
  implicit val hnilInstance: StringFieldLengths[HNil] =
    new StringFieldLengths[HNil] {
      def apply(a: HNil): Map[String, Int] = Map.empty
    }

  implicit def caseClassInstance[A, R <: HList](implicit
    gen: LabelledGeneric.Aux[A, R],
    sfl: StringFieldLengths[R]
  ): StringFieldLengths[A] = new StringFieldLengths[A] {
    def apply(a: A): Map[String, Int] = sfl(gen.to(a))
  }

  implicit def hconsStringInstance[K <: Symbol, T <: HList](implicit
    sfl: StringFieldLengths[T],
    key: Witness.Aux[K]
  ): StringFieldLengths[FieldType[K, String] :: T] =
    new StringFieldLengths[FieldType[K, String] :: T] {
      def apply(a: FieldType[K, String] :: T): Map[String, Int] =
        sfl(a.tail).updated(key.value.name, a.head.length)
    }
}

sealed class LowPriorityStringFieldLengths {
  implicit def hconsInstance[K, V, T <: HList](implicit
    sfl: StringFieldLengths[T]
  ): StringFieldLengths[FieldType[K, V] :: T] =
    new StringFieldLengths[FieldType[K, V] :: T] {
      def apply(a: FieldType[K, V] :: T): Map[String, Int] = sfl(a.tail)
    }
}

这看起来很复杂,但是一旦你开始使用 Shapeless 一点,你就会学会在睡梦中写这种东西。

现在你可以用一种相对简单的方式编写你的操作逻辑:

def maxStringLengths[A: StringFieldLengths](as: List[A]): Map[String, Int] =
  as.map(implicitly[StringFieldLengths[A]].apply).foldLeft(
    Map.empty[String, Int]
  ) {
    case (x, y) => x.foldLeft(y) {
      case (acc, (k, v)) =>
        acc.updated(k, acc.get(k).fold(v)(accV => math.max(accV, v)))
    }
  }

然后(根据rows问题中的定义给出):

scala> maxStringLengths(rows).foreach(println)
(bankCharges,3)
(overTime,3)
(mgmtFee,5)
(email,9)
(copyOfVisa,3)

这绝对适用于任何案例类。

如果这是一次性的事情,您不妨使用运行时反射,或者您可以使用Poly1Giovanni Caporaletti 的回答中的方法——它不太通用,它以我不喜欢的方式混合了解决方案的不同部分,但它应该工作得很好。但是,如果这是你经常做的事情,我会建议我在这里给出的方法。

于 2016-04-06T16:59:16.343 回答
3

如果您想使用 shapeless 来获取案例类的字符串字段并避免反射,您可以执行以下操作:

import shapeless._
import labelled._

trait lowerPriorityfilterStrings extends Poly2 {
  implicit def default[A] = at[Vector[(String, String)], A] { case (acc, _) => acc }
}

object filterStrings extends lowerPriorityfilterStrings {
  implicit def caseString[K <: Symbol](implicit w: Witness.Aux[K]) = at[Vector[(String, String)], FieldType[K, String]] {
    case (acc, x) =>  acc :+ (w.value.name -> x)
  }
}

val gen = LabelledGeneric[CrmContractorRow]


val a = CrmContractorRow(1,"1","1",4444,"1",1,"1","1")
val b = CrmContractorRow(22,"22","22",22,"55555",22,"nine long","22")
val c = CrmContractorRow(333,"333","333",333,"333",333,"333","333")
val rows = List(a,b,c)

val result = rows
  // get for each element a Vector of (fieldName -> stringField) pairs for the string fields
  .map(r => gen.to(r).foldLeft(Vector[(String, String)]())(filterStrings))
  // get the maximum for each "column"
  .reduceLeft((best, row) => best.zip(row).map {
    case (kv1@(_, v1), (_, v2)) if v1.length > v2.length => kv1
    case (_, kv2) => kv2
  })

result foreach { case (k, v) => println(s"$k: $v") }
于 2016-04-06T14:42:23.807 回答
2

您可能想使用 Scala 反射:

import scala.reflect.runtime.universe._

val rm = runtimeMirror(getClass.getClassLoader)
val instanceMirrors = rows map rm.reflect
typeOf[CrmContractorRow].members collect {
  case m: MethodSymbol if m.isCaseAccessor && m.returnType =:= typeOf[String] =>
    val maxValue = instanceMirrors map (_.reflectField(m).get.asInstanceOf[String]) maxBy (_.length)
    println(s"${m.name}: $maxValue")
}

这样您就可以避免以下情况的问题:

case class CrmContractorRow(id: Long, bankCharges: String, overTime: String, name$id: Long, mgmtFee: String, contractDetails$id: Long, email: String, copyOfVisa: String) {
  val unwantedVal = "jdjd"
}

干杯

于 2016-04-06T14:40:01.120 回答
0

我已将您的代码重构为更可重用的代码:

import scala.reflect.ClassTag

case class CrmContractorRow(
                             id: Long,
                             bankCharges: String,
                             overTime: String,
                             name$id: Long,
                             mgmtFee: String,
                             contractDetails$id: Long,
                             email: String,
                             copyOfVisa: String)

object Go{
  def main(args: Array[String]) {
    val a = CrmContractorRow(1,"1","1",4444,"1",1,"1","1")
    val b = CrmContractorRow(22,"22","22",22,"55555",22,"nine long","22")
    val c = CrmContractorRow(333,"333","333",333,"333",333,"333","333")
    val rows = List(a,b,c)
    val initEmptyColumns = List.fill(a.productArity)(List())

    def aggregateColumns[Tin:ClassTag,Tagg](rows: Iterable[Product], aggregate: Iterable[Tin] => Tagg) = {

      val columnsWithMatchingType = (0 until rows.head.productArity).filter {
        index => rows.head.productElement(index) match {case t: Tin => true; case _ => false}
      }

      def columnIterable(col: Int) = rows.map(_.productElement(col)).asInstanceOf[Iterable[Tin]]

      columnsWithMatchingType.map(index => (index,aggregate(columnIterable(index))))
    }

    def extractCaseClassFieldNames[T: scala.reflect.ClassTag] = {
      scala.reflect.classTag[T].runtimeClass.getDeclaredFields.filter(!_.isSynthetic).map(_.getName)
    }

    val agg = aggregateColumns[String,String] (rows,_.maxBy(_.length))
    val fieldNames = extractCaseClassFieldNames[CrmContractorRow]

    agg.map{case (index,value) => fieldNames(index) + ": "+ value}.foreach(println)
  }
}

使用 shapeless 将摆脱 .asInstanceOf,但本质是相同的。给定代码的主要问题是它不可重用,因为聚合逻辑与反射逻辑混合以获取字段名称。

于 2016-04-06T15:27:49.067 回答