Last active
June 18, 2017 02:26
-
-
Save ramannanda9/f751f561a3caa09239905e8dbecbfeab to your computer and use it in GitHub Desktop.
Some Scala Transpose Experiments.
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 scala.annotation.tailrec | |
import scala.collection.mutable.ListBuffer | |
case class Priceable(id: String,measure:String,value: Double) | |
val results=Seq( | |
Priceable("$APPL","PV",311.0), | |
Priceable("$APPL","Growth",3.0), | |
Priceable("$IBM","PV",100), | |
Priceable("$IBM","Growth",0.5), | |
Priceable("$ORCL","PV",45.3), | |
Priceable("$ORCL","Growth",2.0) | |
) | |
val grouped=results.groupBy(_.id) | |
val name=grouped.head._1 | |
val base=grouped.get(name).get.map(p=>Seq(p.measure,p.id,p.value)) | |
val others=grouped.filterKeys(_!=name).values.map(k=>k.map(p=>Seq(p.id,p.value))) | |
val entries=Seq(base,merge(others)) | |
merge(entries = entries) | |
def merge(entries:Iterable[Seq[Seq[Any]]]):Seq[Seq[Any]] = { | |
val entryArray = entries.toArray | |
for (i <- Array.range(0, entries.head.size)) yield { | |
val lb = new ListBuffer[Seq[Any]] | |
for (entry <- entryArray) | |
lb+=entry(i) | |
lb.flatten | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment