Created
August 23, 2017 15:23
-
-
Save fokot/5bd321c3efb4eeda15a4103ed0e3b79e to your computer and use it in GitHub Desktop.
Shapeless - two classes to list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import shapeless._ | |
import shapeless.labelled.{FieldType, field} | |
import shapeless.ops.hlist.{Align, ToTraversable, ZipWith} | |
object ShapelessTest { | |
def zipClasses[A, B, P <: Poly2, ARepr <: HList, BRepr <: HList, R <: HList, X] | |
(a: A, b: B, f: P)( | |
implicit | |
aGen : LabelledGeneric.Aux[A, ARepr], | |
bGen : LabelledGeneric.Aux[B, BRepr], | |
// align : Align[ARepr, BRepr], | |
zipWith : ZipWith.Aux[ARepr, BRepr, P, R], | |
toTrav: ToTraversable.Aux[R, List, X] | |
): List[X] = | |
aGen.to(a).zipWith(bGen.to(b))(f).toList | |
// align.apply(aGen.to(a)).zipWith(bGen.to(b))(f).toList | |
def main(args: Array[String]): Unit = { | |
case class Column[A](value: A) | |
case class DTable(id: Column[Long], s2: Column[Int], s: Column[String]) | |
case class DFilter(id: Option[Long], s: Option[String], s2: Option[Int]) | |
object filter extends Poly2 { | |
implicit def repr[K <: Symbol, V] = at[FieldType[K, Column[V]], FieldType[K, Option[V]]] { (a, b) => | |
field[K]( (a, b).asInstanceOf[(Any, Any)] ) | |
} | |
} | |
val dTable = new DTable(Column(1L), Column(3), Column("s")) | |
val dFilter = new DFilter(Option(222L), Option("second"), Option(234)) | |
// def filter[A](col: Column[A], filterCriteria: Option[A]): Option[Boolean] = ??? | |
// def filterClass(table: DTable, filter: DFilter): List[Option[Boolean]] = ??? | |
def filterClass(t: DTable, f: DFilter): List[(Any, Any)] = zipClasses(t, f, filter) | |
val res = filterClass(dTable, dFilter) | |
println(res) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment