3

Possible Duplicate:
Use 'map' and stuff on Scala Tuples?

Why cannot I iterate over this construct (I am not sure how to call it, since Scala just calls it (Int, Int, Int))?

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

The code above produces the following error:

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

Community
  • 1
  • 1
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149

3 Answers3

5

You can use .productIterator or .productElements for such things:

t.productElements.toList.map(println)

I've used toList to strict operation, cause productIterator returns Iterator which is lazy.

Tip: it is recommended to use .foreach for functions without result (those that produce side effects, just like println do)

t.productElements.toList.foreach(println)
om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
4

I got it.

It's called a "tuple", and it's already answered here.

Use 'map' and stuff on Scala Tuples?

Community
  • 1
  • 1
Karel Bílek
  • 36,467
  • 31
  • 94
  • 149
  • 1
    I added an answer to that question with a pointer to [shapeless](https://github.com/milessabin/shapeless) which might be useful. – Miles Sabin May 07 '12 at 18:59
3

Based on on the name of your value list, it seems like you meant to use a List instead of a Tuple. Try this to create a List which defines map:

List(1,2,3).map{println _}
drstevens
  • 2,903
  • 1
  • 21
  • 30
ie.
  • 5,982
  • 1
  • 29
  • 44
  • 1
    could you please define the minus? – ie. May 07 '12 at 15:26
  • 1
    That was me, sorry about late reasoning. I've downvoted, cause *use another type/collection* is not appropriate general solution, IMO. You may have a tuple given by API (e.g. returned from function), or some other restriction, when you just can't write List(...) instead of (...) ) – om-nom-nom May 07 '12 at 17:31
  • 1
    +1 - It is very possible based on the value name `list` that in the original question, that a List was intended instead of a Tuple. Because of this, I feel that this is a valid answer. I edited to make it more clear what @ie is answering. The original question should be edited as well. – drstevens May 07 '12 at 19:15