2016-12-07 5 views
0

Ich versuche, Listen mit den folgenden Funktionen zu kombinieren:Scala reduceList wegen Typenkonflikt Fehler java.io.Serializable erfordern

def merge(first: List[(Char, Int)], other: List[(Char, Int)]): List[List[(Char, Int)]] = 
    first.flatMap(tpl => other map (tpl2 => List(tpl, tpl2))) 

def combine(l: List[List[(Char, Int)]]): List[List[(Char, Int)]] = l reduceLeft merge 

Leider bekomme ich die folgende Compiler-Nachricht:

Error: type mismatch;
found: (List[(Char, Int)], List[(Char, Int)]) => List[List[(Char, Int)]]
required: (List[Product with java.io.Serializable], List[(Char, Int)]) => List[Product with java.io.Serializable]
l reduceLeft merge

Ich verstehe, dass die Reduzierung einer List[Int] kann nur ein Ergebnis von Int erzeugen. In meinem Fall habe ich List[List[(Char, Int)]], also erwarte ich, dass ich ein Ergebnis von List[(Char, Int)] erzeugen kann. Kann mir jemand helfen zu verstehen, was mit meinem Code nicht stimmt?

+0

Sie mappen über andere und fügen 'tpl' zu allen Elementen der' anderen' Tupel-Liste hinzu? –

+0

ja ich bin so, dass, wenn zuerst = Liste (('a', 1)) und andere = Liste (('b', 1), ('b', 2)) es Liste (Liste (('a ', 1), (' b ', 1)), Liste ((' a ', 1), (' b ', 2))) – Tundebabzy

Antwort

1

Definition von reduceLeft:

def reduceLeft[B >: A](op: (B, A) ⇒ B): B 

So merge Ihre Funktion List[(Char, Int)] nicht List[List[(Char, Int)]] zurückkehren. Auch Ihre Funktion combine wird List[(Char, Int)] zurückgeben, da sie List List in List reduziert.

Verwandte Themen