mardi 21 avril 2015

Map tuples to tuples using Iterator

Why is the following code does not work, and how can I overcome it using Iterator?

def f(str : String) : (String, String) = {
  str.splitAt(1)
}
var with_id : Iterator[(String, Int)] = List(("test", 1), ("list", 2), ("nothing", 3), ("else", 4)).iterator

println(with_id.mkString(" "))

val result = with_id map { (s : String, i : Int) => (f(s), i) }

println(result.mkString(" "))

Expected output is:

(("t", "est"), 1) (("l", "ist"), 2) ...

Error:

Error:(28, 54) type mismatch;
found   : (String, Int) => ((String, String), Int)
required: ((String, Int)) => ?
val result = with_id map { (s : String, i : Int) => (f(s), i) }
                                                 ^

Aucun commentaire:

Enregistrer un commentaire