문제

Let's say I have 3 tuple sequences that look something like this:

Seq("m1" -> 1, "m2" -> 2)
Seq("m3" -> 3, "m4" -> 4)
Seq("m5" -> 5, "m2" -> 6)

I would like to map over these and return 3 new records that look something like the following:

Seq("m1" -> Some(1), "m2" -> Some(2), "m3" -> None, "m4" -> None, "m5" -> None)
Seq("m1" -> None, "m2" -> None, "m3" -> Some(3), "m4" -> Some(4), "m5" -> None)
Seq("m1" -> None, "m2" -> Some(6), "m3" -> None, "m4" -> None, "m5" -> Some(5))

The new collection that I'm looking for contains a key for the distinct set of keys from the original list and values of Some(v) or None based on whether or not the corresponding original sequence contained the tuple.

I managed to pull out the keys from the original list:

case class SiteReading(val site: String, val measures: Seq[(String, Double)])
val xs = getSomeSiteReadingsFromSomewhere()
val keys = xs.flatMap(_.measures.map(t => t._1)).distinct

I now plan to go through the whole list again, generating a new list by looking at both the current value and the unique set of keys. I'm wondering if there are some nifty things in the collections framework that makes this a little cleaner and more tractable? Maybe point-free?

도움이 되었습니까?

해결책

val s1 = Seq("m1" -> 1, "m2" -> 2)
val s2 = Seq("m3" -> 3, "m4" -> 4)
val s3 = Seq("m5" -> 5, "m2" -> 6)
val ss = Seq(s1, s2, s3)
def foo(xss: Seq[Seq[(String,Int)]]): Seq[Seq[(String,Option[Int])]] = {
  val keys = xss.flatMap(_.map(_._1)).toSet
  xss.map{ xs => 
    val found = xs.map{ case (s,i) => (s, Some(i)) }
    val missing = (keys diff xs.map(_._1).toSet).map(x => (x, None)).toSeq
    (found ++ missing).sortBy(_._1)
  }
}

scala> foo(ss).foreach(println)
List((m1,Some(1)), (m2,Some(2)), (m3,None), (m4,None), (m5,None))
List((m1,None), (m2,None), (m3,Some(3)), (m4,Some(4)), (m5,None))
List((m1,None), (m2,Some(6)), (m3,None), (m4,None), (m5,Some(5)))

다른 팁

Here's an approach. Map over the keys and query each map to see if it contains the key.

Make a set of keys to iterate over.

scala> val ms = (1 to 5).map(i => "m" + i)
ms: scala.collection.immutable.IndexedSeq[String] = Vector(m1, m2, m3, m4, m5)

Three tuple sequences

scala> val s1 = Seq("m1" -> 1, "m2" -> 2).toMap
s1: scala.collection.immutable.Map[String,Int] =  Map(m1 -> 1, m2 -> 2)

scala> val s2 = Seq("m3" -> 3, "m4" -> 4).toMap
s2: scala.collection.immutable.Map[String,Int] = Map(m3 -> 3, m4 -> 4)

scala> val s3 = Seq("m5" -> 5, "m2" -> 6).toMap
s3: s3: scala.collection.immutable.Map[String,Int] = Map(m5 -> 5, m2 -> 6)

map the Seq of keys over each Set and try to get the key.

scala> ms.map(m => m -> s1.get(m))
res19: scala.collection.immutable.IndexedSeq[(String, Option[Int])] = 
Vector((m1,Some(1)), (m2,Some(2)), (m3,None), (m4,None), (m5,None))

scala> ms.map(m => m -> s2.get(m))
res20: scala.collection.immutable.IndexedSeq[(String, Option[Int])] = 
Vector((m1,None), (m2,None), (m3,Some(3)), (m4,Some(4)), (m5,None))

scala> ms.map(m => m -> s3.get(m))
res21: scala.collection.immutable.IndexedSeq[(String, Option[Int])] = 
Vector((m1,None), (m2,Some(6)), (m3,None), (m4,None), (m5,Some(5)))

I like Rex Kerr's answer. I commented that this solution also works well and is potentially more clear and concise.

def denormalize(xss: Seq[Seq[(String, Double)]]): Seq[Map[String, Option[Double]]] = {
  val keys = xss.flatMap(_.map(_._1)).distinct.sorted
  val base = keys.map(_ -> None).toMap[String, Option[Double]]
  xss.map(base ++ _.map(t => t._1 -> Option(t._2)))
}

It would work equally as well as a set. I'm not sure which performs better. I might test both.

Here is my solution:

val s1 = Seq("m1" -> 1, "m2" -> 2)
val s2 = Seq("m3" -> 3, "m4" -> 4)
val s3 = Seq("m5" -> 5, "m2" -> 6)

def process(ss: Seq[(String, Int)]*): Seq[Seq[(String, Option[Int])]] = {
  val asMap = ss map (_.toMap)
  val keys = asMap.flatMap(_.keys).sorted
  for(m <- asMap) yield keys.map(k => k -> m.get(k)) 
}

val Seq(r1, r2, r3) = process(s1, s2, s3)

Result:

r1: Seq[(String, Option[Int])] = ArrayBuffer((m1,Some(1)), (m2,Some(2)), (m2,Some(2)), (m3,None), (m4,None), (m5,None))
r2: Seq[(String, Option[Int])] = ArrayBuffer((m1,None), (m2,None), (m2,None), (m3,Some(3)), (m4,Some(4)), (m5,None))
r3: Seq[(String, Option[Int])] = ArrayBuffer((m1,None), (m2,Some(6)), (m2,Some(6)), (m3,None), (m4,None), (m5,Some(5)))
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top