igx igx - 4 years ago 93
Scala Question

scala create new list by omitting the n element from existing list with tuples

I new in scala so please try to elaborate.

I have a list such as:

val trioElmList = List((10,20,30),(50,89,90),(60,40,65))

How can I get a new list with the middle element of each tuple omitted? The result should be:

newList : List[(Int, Int)] = List((10,30), (50,90), (60,65))

Answer Source

You can use the map method to apply a function to each element of the list and create a new list with the return values of that function. In the function, you use pattern matching to get the elements of the tuple and return a tuple with only the first and last element.

trioElmList.map { case (x,y,z) => (x,z) }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download