This page is no longer maintained — Please continue to the home page at www.scala-lang.org

Alternate syntaxes for iterating over a map?

6 replies
Ikai Lan
Joined: 2009-03-15,
User offline. Last seen 42 years 45 weeks ago.
Supposed I have a map:
val map = Map( 1 -> "one", 2 -> "two")
What are the different syntaxes for iterating over it? Say I want to print the value and key of each member of the map. I've discovered these syntaxes:
map.foreach( println(_._1 + " " + _._2 ) )
map.foreach( (num) => println(num._1 + " " + num._2)
Are there any alternative syntaxes to the _1 and _2 to reference the key and the value?
Bryan
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Alternate syntaxes for iterating over a map?
map.foreach(x => x match { case (a, b) => println(a + " " + b) })

map.foreach({case (a, b) => println(a + " " + b) })

On Sat, Mar 14, 2009 at 8:00 PM, Ikai Lan <ikai.lan@gmail.com> wrote:
Supposed I have a map:
val map = Map( 1 -> "one", 2 -> "two")
What are the different syntaxes for iterating over it? Say I want to print the value and key of each member of the map. I've discovered these syntaxes:
map.foreach( println(_._1 + " " + _._2 ) )
map.foreach( (num) => println(num._1 + " " + num._2)
Are there any alternative syntaxes to the _1 and _2 to reference the key and the value?

DRMacIver
Joined: 2008-09-02,
User offline. Last seen 42 years 45 weeks ago.
Re: Alternate syntaxes for iterating over a map?

On Sun, Mar 15, 2009 at 12:21 AM, Bryan wrote:
> map.foreach(x => x match { case (a, b) => println(a + " " + b) })
>
> map.foreach({case (a, b) => println(a + " " + b) })

Note that the ()s are unnecessary there.

map.foreach{case (a, b) => println(a + " " + b) }

works fine.

extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: Alternate syntaxes for iterating over a map?

On Sat, Mar 14, 2009 at 05:00:04PM -0700, Ikai Lan wrote:
> Supposed I have a map:
> val map = Map( 1 -> "one", 2 -> "two")
>
> What are the different syntaxes for iterating over it?

The main two I would use are:

scala> for ((k, v) <- map) println(k + " " + v)
1 one
2 two

scala> map foreach { case (k,v) => println(k + " " + v) }
1 one
2 two

sadie
Joined: 2008-12-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Alternate syntaxes for iterating over a map?

It'd be nice if the syntax could be made so iteration reflected creation,
with the minimum of extra symbols:

map foreach { a -> b => println(a + " " + b) }

In other words, make a -> b an alias for (a, b) in patterns as well as
values. But that would probably interfere with other meanings of the symbol.
In reality I'd probably go for the conceptually closest:

map foreach { case (a, b) => println(a + " " + b) }

It feels unscalaish to have to manually pull components out with ._1 and ._2

James Iry
Joined: 2008-08-19,
User offline. Last seen 1 year 23 weeks ago.
Re: Alternate syntaxes for iterating over a map?
scala> object -> { def unapply[A,B](x : (A,B)) : Option[(A,B)] = Some(x) }
defined module $minus$greater

scala> val map = Map(1 -> 2, 3 -> 4) 
map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4)

scala> map foreach {case a -> b => println(a + " " + b)}
1 2
3 4


On Sat, Mar 14, 2009 at 7:46 PM, Marcus Downing <marcus@minotaur.it> wrote:

It'd be nice if the syntax could be made so iteration reflected creation,
with the minimum of extra symbols:

 map foreach { a -> b => println(a + " " + b) }

In other words, make a -> b an alias for (a, b) in patterns as well as
values. But that would probably interfere with other meanings of the symbol.
In reality I'd probably go for the conceptually closest:

 map foreach { case (a, b) => println(a + " " + b) }

It feels unscalaish to have to manually pull components out with ._1 and ._2
--
View this message in context: http://www.nabble.com/Alternate-syntaxes-for-iterating-over-a-map--tp22518702p22519569.html
Sent from the Scala - User mailing list archive at Nabble.com.


sadie
Joined: 2008-12-21,
User offline. Last seen 42 years 45 weeks ago.
Re: Alternate syntaxes for iterating over a map?

James Iry-2 wrote:
>
> scala> object -> { def unapply[A,B](x : (A,B)) : Option[(A,B)] = Some(x) }
> defined module $minus$greater
>
> scala> val map = Map(1 -> 2, 3 -> 4)
> map: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2, 3 -> 4)
>
> scala> map foreach {case a -> b => println(a + " " + b)}
> 1 2
> 3 4
>
That's... actually quite frightening. As a Java programmer, I can feel the
world moving beneath my feet. :D

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland