Question

Imagine that we have a list of tuples such as:

List((5,4), (3,2), (1,0))

How would you multiply the values in each tuple with themselves then combine the products into a single list?

List(20, 6, 1)

I believe there is a way to do so using Scala's built-in map function, but I'm unsure of how to do so.

Was it helpful?

Solution

val list = List((5,4), (3,2), (1,0))
list.map { case (a, b) => a * b }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top