Skip to content

Instantly share code, notes, and snippets.

@coderunner
Last active December 16, 2015 08:58
Show Gist options
  • Select an option

  • Save coderunner/5409233 to your computer and use it in GitHub Desktop.

Select an option

Save coderunner/5409233 to your computer and use it in GitHub Desktop.

Revisions

  1. Felix Trepanier revised this gist Apr 18, 2013. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions collapse.scala
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ object CaseToList extends App {

    // Then use a generic method that filters out None and extract values from Some and let other types go by unchanged.
    // Here I zip with numbers to have tuple of the form (1 -> value).
    def toCollapsedList: Seq[Any] = {
    def toCollapsedList2: Seq[Any] = {
    val values = toList
    for (pair <- (1 to values.size).zip(values) if pair._2 != None) yield pair match {
    case (id, Some(v)) => (id -> v)
    @@ -38,6 +38,6 @@ object CaseToList extends App {
    val q1 = Query("allo", None, Some(3.0))

    println(q1.toCollapsedList1.mkString)
    println(q1.toCollapsedList.mkString)
    println(q1.toCollapsedList2.mkString)

    }
  2. Felix Trepanier revised this gist Apr 18, 2013. 1 changed file with 9 additions and 1 deletion.
    10 changes: 9 additions & 1 deletion collapse.scala
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,10 @@ object CaseToList extends App {

    trait Collapsable {

    // OPTION 1
    // Use reflection on the fields using the known public method of the property to access its value.
    // Can be incorrect if the case class defines attributes not to be added to the list.
    // Here I use the property name in the returned tuple list.
    def toCollapsedList1: Seq[Any] = {
    val list = for {
    f <- this.getClass.getDeclaredFields
    @@ -12,12 +16,16 @@ object CaseToList extends App {
    }
    }

    // OPTION 2
    // Define an abstract method that convert the desired properties to a List
    def toList: List[Any]

    // Then use a generic method that filters out None and extract values from Some and let other types go by unchanged.
    // Here I zip with numbers to have tuple of the form (1 -> value).
    def toCollapsedList: Seq[Any] = {
    val values = toList
    for (pair <- (1 to values.size).zip(values) if pair._2 != None) yield pair match {
    case (id, Some(vv)) => (id -> vv)
    case (id, Some(v)) => (id -> v)
    case (id, x) => (id -> x)
    }
    }
  3. Felix Trepanier created this gist Apr 18, 2013.
    35 changes: 35 additions & 0 deletions collapse.scala
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    object CaseToList extends App {

    trait Collapsable {

    def toCollapsedList1: Seq[Any] = {
    val list = for {
    f <- this.getClass.getDeclaredFields
    } yield (f.getName -> this.getClass.getDeclaredMethod(f.getName).invoke(this))
    list.filter(_._2 != None).map {
    case (id, Some(v)) => (id -> v)
    case (id, x) => (id -> x)
    }
    }

    def toList: List[Any]

    def toCollapsedList: Seq[Any] = {
    val values = toList
    for (pair <- (1 to values.size).zip(values) if pair._2 != None) yield pair match {
    case (id, Some(vv)) => (id -> vv)
    case (id, x) => (id -> x)
    }
    }
    }

    case class Query(c1: String, c2: Option[String], c3: Option[Double]) extends Collapsable {
    def toList = c1 :: c2 :: c3 :: Nil
    }

    val q1 = Query("allo", None, Some(3.0))

    println(q1.toCollapsedList1.mkString)
    println(q1.toCollapsedList.mkString)

    }