Converting two lists to type : List[List[scala.collection.immutable.Map[String,String]]] -
have 2 lists :
val list1 : list[list[(string, string)]] = list(list("1" -> "a" , "1" -> "b")) //> list1 : list[list[(string, string)]] = list(list((1,a), (1,b))) val list2 : list[list[(string, string)]] = list(list("2" -> "a" , "2" -> "b")) //> list2 : list[list[(string, string)]] = list(list((2,a), (2,b))) //expecting val toconvert = list(list(map("1" -> "a" , "2" -> "b"), map("1" -> "b" , "2" -> "a"))) attempting convert these lists type :
list[list[scala.collection.immutable.map[string,string]]] = lis //| t(list(map(1 -> a, 2 -> b), map(1 -> b, 2 -> a))) using code :
val ll = list1.zip(list2).map(m => list(m._1.tomap , m._2.tomap)) but map entries missing :
list[list[scala.collection.immutable.map[string,string]]] = list(list( //| map(1 -> b), map(2 -> b))) how convert list1,list2 type list[list[scala.collection.immutable.map[string,string]]] includes values : (list(map(1 -> a, 2 -> b), map(1 -> b, 2 -> a))) ?
update :
logic of : map("1" -> "a" , "2" -> "b")
combine each list using zip functions :
val p1 : list[(list[(string, string)], list[(string, string)])] = list1.zip(list2); convert each element of thenewly created list map , add newly created list :
val p2 = p1.map(m => list(m._1.tomap , m._2.tomap))
how convert
list1,list2typelist[list[map[...
starting type list[list[(string, string)]] type list[list[map[string, string]]].
inner type
the inner type want map[string, string]. asked in comments, don't understand expected logic construct map assuming want create map[string, string] list of tuples list[(string, string)].
when creating map using .tomap, key-value elements same key overwritten can see b += x in implementation:
def tomap[t, u](implicit ev: <:< (t, u)): immutable.map[t, u] = { val b = immutable.map.newbuilder[t, u] (x <- self) b += x b.result() } (source traversableonce.scala)
so logic use create list[(string, string)] determine generated map[string, string].
outer list of list
combine each list using zip functions
type = list[(string, string)] val list12: list[(a, a)] = list1 zip list2 zipping list1 , list2 gives list of tuples type want list of list of tuple: list[list[(a, a)]]
in example getting type mapping list m => list(...). key part. let me split in 2 parts make clearer:
list12.map(m => list(m._1 , m._2)).map(_.map(_.tomap)) let's extract separate function:
def keypart: ((a, a)) => list[a] = { case (l1, l2) => list(l1, l2) } val resultnotexpected = list12.map(keypart).map(_.map(_.tomap)) the result of course same of 1 in question:
resultnotexpected == list(list(map("1" -> "b"), map("2" -> "b"))) the "keypart"
in question mentioned expected result as:
list(list(map("1" -> "a", "2" -> "b"), map("1" -> "b", "2" -> "a"))) i still don't understand logic have in mind extracted keypart function give mine... over-complex of course:
val resultexpected = list12.map { case (l1, l2) => list(l1, l2) } .map(_.map(_.zipwithindex)).map(_.zipwithindex) .map(_.map { case (l, i) => l.map { case ((k, v), j) => (k, v, i, j) } }).map(_.flatten) .map(_.groupby { case (k, v, i, j) => == j }).map(_.values.tolist) .map(_.map(_.map { case (k, v, _, _) => k -> v }.sorted).sortby(_.head._2)) .map(_.map(_.tomap)) scala> resultexpected == list(list(map("1" -> "a", "2" -> "b"), map("1" -> "b", "2" -> "a"))) boolean = true perhaps know better logic implemented in keypart.
Comments
Post a Comment