How to create seperate lists from a list using scala? -



How to create seperate lists from a list using scala? -

i have next list -

list((name1,a1,176980), (name2,a2,0), (name3,a3,1948), (name4,a4,95676))

from above list want create separate lists of list elements like- element1, element2 , element3 respectively.

i want separate lists like-

list(name1,name2,name3,name4) list(a1,a2,a3,a4) list(176980,0,1948,95676)

how above lists using scala???

naive solution:

val list = list( ("name1","a1",176980), ("name2","a20",0), ("name3","a3",1948), ("name4","a4",95676)) list.map(_._1) list.map(_._2) list.map(_._3)

some generalize version:

def key(products: list[product], num: int) = { products.map(_.productelement(num)) } key(list, 0) // res3: list[any] = list(name1, name2, name3, name4) key(list, 1) // res4: list[any] = list(a1, a20, a3, a4) key(list, 2) // res5: list[any] = list(176980, 0, 1948, 95676)

or product arity:

def key(products: list[product], num: int) = { products.map { p => option(p) .filter(_.productarity > num) .map(_.productelement(num)) .getorelse(none) } }

list scala collections scala-collections

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -