Friday, October 18, 2019

More Scala case classes converted to nested tuples converted to nested maps

   case class X(a: String, b: String, c: String, d: String )
  val x = Seq(X("a", "b", "c", "d"), X("a", "b", "p", "q") )
  x.map{case X(a,b,c,d) => (a -> (b -> (c, d)))}.groupBy(_._1)
  x.map{case X(a,b,c,d) => (a -> (b -> (c, d)))}.groupBy(_._1).map{case (k,v) => (k -> v.map(_._2))}
  x.map{case X(a,b,c,d) => (a -> (b -> (c, d)))}.groupBy(_._1).map{case (k,v) => (k -> v.map(_._2).groupBy(_._1).map{case (k,v) => (k -> v.map(_._2))})}
x.map{case X(a,b,c,d) => (a -> (b -> (c, d)))}.groupBy(_._1).map{case (k,v) => (k -> v.map(_._2).groupBy(_._1).map{case (k,v) => (k -> v.map(_._2).groupBy(_._1).flatMap(_._2))})}



scala>  case class X(a: String, b: String, c: String, d: String )
defined class X
scala>         val x = Seq(X("a", "b", "c", "d"), X("a", "b", "p", "q") )
x: Seq[X] = List(X(a,b,c,d), X(a,b,p,q))
scala>         x.map{case X(a,b,c,d) => (a -> (b -> (c, d)))}.groupBy(_._1)
res140: scala.collection.immutable.Map[String,Seq[(String, (String, (String, String)))]] = Map(a -> List((a,(b,(c,d))), (a,(b,(p,q)))))
scala>         x.map{case X(a,b,c,d) => (a -> (b -> (c, d)))}.groupBy(_._1).map{case (k,v) => (k -> v.map(_._2))}
res141: scala.collection.immutable.Map[String,Seq[(String, (String, String))]] = Map(a -> List((b,(c,d)), (b,(p,q))))
scala>         x.map{case X(a,b,c,d) => (a -> (b -> (c, d)))}.groupBy(_._1).map{case (k,v) => (k -> v.map(_._2).groupBy(_._1).map{case (k,v) => (k -> v.map(_._2))})}
res142: scala.collection.immutable.Map[String,scala.collection.immutable.Map[String,Seq[(String, String)]]] = Map(a -> Map(b -> List((c,d), (p,q))))
scala>       x.map{case X(a,b,c,d) => (a -> (b -> (c, d)))}.groupBy(_._1).map{case (k,v) => (k -> v.map(_._2).groupBy(_._1).map{case (k,v) => (k -> v.map(_._2).groupBy(_._1).flatMap(_._2))})}
res143: scala.collection.immutable.Map[String,scala.collection.immutable.Map[String,scala.collection.immutable.Map[String,String]]] = Map(a -> Map(b -> Map(p -> q, c -> d)))

No comments:

Post a Comment