Why can't I iterate over (Int, Int, Int) using a map?

Possible duplicate:
Use 'map' and more in Scala Tuples?

Why can't I iterate over this construct (I'm not sure what to call it, since Scala just calls it (Int, Int, Int))?

val list = (1,2,3)
list.map{println _}

In the above code, the following error occurs:

<console>: 9: error: value map is not a member (Int, Int, Int)
(1,2,3) .map {println _}

+3
source share
3 answers

You can use .productIteratoror .productElementsfor things like this:

t.productElements.toList.map(println)

I used toList for rigorous work, because productIterator returns an Iterator, which is lazy.

: .foreach (, , , println)

t.productElements.toList.foreach(println)
+5
+4

Depending on the name of your value list, it looks like you used List instead of Tuple . Try creating listone that defines map:

List(1,2,3).map{println _}
+3
source

All Articles