Skip to content

Instantly share code, notes, and snippets.

@stsatlantis
Last active June 24, 2018 14:27
Show Gist options
  • Save stsatlantis/ce4f6b26afe8b461424f566ef1211f3a to your computer and use it in GitHub Desktop.
Save stsatlantis/ce4f6b26afe8b461424f566ef1211f3a to your computer and use it in GitHub Desktop.
Lists the field names where the values are different
object filterPoly extends Poly1 {
implicit def filterNotEqualValues[A <: Symbol, B]: Case.Aux[(A, (B, B)), List[String]] =
at[(A, (B, B))] { case (k, (v1, v2)) => if (v1 == v2) List.empty[String] else List(k.name) }
}
object foldListPoly extends Poly2 {
implicit def foldToList[T](implicit st: filterPoly.Case.Aux[T, List[String]]): Case.Aux[T, List[String], List[String]] =
at[T, List[String]] { (t, acc) => st(t) ::: acc }
}
def diffFields[T, LHL <: HList, K <: HList, KOut <: HList, ZOut <: HList, ZKOut <: HList](a: T, b: T)(
implicit
gen: LabelledGeneric.Aux[T,LHL]
keys: Keys.Aux[LHL, K],
zipWith: Zip.Aux[LHL :: LHL :: HNil, ZOut],
zipWithKeys: Zip.Aux[K ::ZOut :: HNil, ZKOut],
rightFolder: RightFolder.Aux[ZKOut, List[String], foldListPoly.type, List[String]]
): Seq[String] = {
keys().zip(gen.to(a).zip(gen.to(b))).foldRight(Nil: List[String])(foldListPoly)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment