- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Alternate syntaxes for iterating over a map?
Sun, 2009-03-15, 01:00
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?
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?
Sun, 2009-03-15, 01:57
#2
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.
Sun, 2009-03-15, 02:27
#3
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
Sun, 2009-03-15, 03:57
#4
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
Sun, 2009-03-15, 04:07
#5
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:
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.
Sun, 2009-03-15, 04:17
#6
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
map.foreach({case (a, b) => println(a + " " + b) })
On Sat, Mar 14, 2009 at 8:00 PM, Ikai Lan <ikai.lan@gmail.com> wrote: