Last active
December 16, 2015 08:58
-
-
Save coderunner/5409233 to your computer and use it in GitHub Desktop.
Revisions
-
Felix Trepanier revised this gist
Apr 18, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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 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.toCollapsedList2.mkString) } -
Felix Trepanier revised this gist
Apr 18, 2013 . 1 changed file with 9 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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(v)) => (id -> v) case (id, x) => (id -> x) } } -
Felix Trepanier created this gist
Apr 18, 2013 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal 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) }