- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
how do I initialize an immutable map programmatically?
Fri, 2011-12-16, 03:23
Suppose I have lists of indices (keys) and values
and I want to make an immutable k->v map.
How can I do this?
Here's my first try:
val mymap = for ( (i,v) <- ndx.zip(vals) ) yield { i -> v}
But this fails, as shown in this example:
val ndx = List(1,3)
val vals = List(0.5, 0.6)
val mymap = for ( (i,v) <- ndx.zip(vals) ) yield { i -> v}
// List[(Int, Double)] = List((1,0.5), (3,0.6))
So I ended up using this ugly (but correct) imperative code:
val mapping = mutable.Map[Int, Double]()
for ( (i,v) <- ndx.zip(vals)) { mapping(i) = v }
val mymap = mapping.toMap
// scala.collection.immutable.Map[Int,Double] = Map(3 -> 0.6, 1 ->
0.5)
Is there a better way?
Tx
Kevin
Fri, 2011-12-16, 04:01
#2
Re: how do I initialize an immutable map programmatically?
Also, if you specify the return type, you can also skip the intermediate creation of a list of 2-tuples by using collection.breakOut:
val mymap: Map[Int,Double] = ndx.zip(vals)(collection.breakOut)
On Thu, Dec 15, 2011 at 9:23 PM, Kevin Murphy <murphyk2@gmail.com> wrote:
val mymap: Map[Int,Double] = ndx.zip(vals)(collection.breakOut)
On Thu, Dec 15, 2011 at 9:23 PM, Kevin Murphy <murphyk2@gmail.com> wrote:
Suppose I have lists of indices (keys) and values
and I want to make an immutable k->v map.
How can I do this?
Here's my first try:
val mymap = for ( (i,v) <- ndx.zip(vals) ) yield { i -> v}
But this fails, as shown in this example:
val ndx = List(1,3)
val vals = List(0.5, 0.6)
val mymap = for ( (i,v) <- ndx.zip(vals) ) yield { i -> v}
// List[(Int, Double)] = List((1,0.5), (3,0.6))
So I ended up using this ugly (but correct) imperative code:
val mapping = mutable.Map[Int, Double]()
for ( (i,v) <- ndx.zip(vals)) { mapping(i) = v }
val mymap = mapping.toMap
// scala.collection.immutable.Map[Int,Double] = Map(3 -> 0.6, 1 ->
0.5)
Is there a better way?
Tx
Kevin
Fri, 2011-12-16, 08:31
#3
Re: how do I initialize an immutable map programmatically?
Another option: Map(ndx zip vals: _*)
On Thu, Dec 15, 2011 at 9:52 PM, Tom Switzer <thomas.switzer@gmail.com> wrote:
On Thu, Dec 15, 2011 at 9:52 PM, Tom Switzer <thomas.switzer@gmail.com> wrote:
Also, if you specify the return type, you can also skip the intermediate creation of a list of 2-tuples by using collection.breakOut:
val mymap: Map[Int,Double] = ndx.zip(vals)(collection.breakOut)
On Thu, Dec 15, 2011 at 9:23 PM, Kevin Murphy <murphyk2@gmail.com> wrote:
Suppose I have lists of indices (keys) and values
and I want to make an immutable k->v map.
How can I do this?
Here's my first try:
val mymap = for ( (i,v) <- ndx.zip(vals) ) yield { i -> v}
But this fails, as shown in this example:
val ndx = List(1,3)
val vals = List(0.5, 0.6)
val mymap = for ( (i,v) <- ndx.zip(vals) ) yield { i -> v}
// List[(Int, Double)] = List((1,0.5), (3,0.6))
So I ended up using this ugly (but correct) imperative code:
val mapping = mutable.Map[Int, Double]()
for ( (i,v) <- ndx.zip(vals)) { mapping(i) = v }
val mymap = mapping.toMap
// scala.collection.immutable.Map[Int,Double] = Map(3 -> 0.6, 1 ->
0.5)
Is there a better way?
Tx
Kevin
Fri, 2011-12-16, 18:41
#4
Re: how do I initialize an immutable map programmatically?
Thanks for all these helpful replies.
I found the same question (and answer) here
http://stackoverflow.com/questions/2763584/how-would-i-yield-an-immutabl...
Using the factor Map constructor (rather than toMap method)
has the advantage that we can also create mutable maps,
eg
scala> mutable.Map((ndx zip vals):_*)
res11: scala.collection.mutable.Map[Int,Double] = Map(3 -> 0.6, 1 ->
0.5)
Apparently "the :_* is needed to give the compiler a hint to use the
list as a varargs argument"
(which I interpret to be like using m{:} instead of m(:) in Matlab,
FWIW)
A pretty complex discussion of breakOut is given here:
http://stackoverflow.com/questions/1715681/scala-2-8-breakout
Kevin
On Dec 15, 11:24 pm, Naftoli Gugenheim wrote:
> Another option: Map(ndx zip vals: _*)
>
> On Thu, Dec 15, 2011 at 9:52 PM, Tom Switzer wrote:
>
>
>
>
>
>
>
> > Also, if you specify the return type, you can also skip the intermediate
> > creation of a list of 2-tuples by using collection.breakOut:
>
> > val mymap: Map[Int,Double] = ndx.zip(vals)(collection.breakOut)
>
> > On Thu, Dec 15, 2011 at 9:23 PM, Kevin Murphy wrote:
>
> >> Suppose I have lists of indices (keys) and values
> >> and I want to make an immutable k->v map.
> >> How can I do this?
>
> >> Here's my first try:
>
> >> val mymap = for ( (i,v) <- ndx.zip(vals) ) yield { i -> v}
>
> >> But this fails, as shown in this example:
>
> >> val ndx = List(1,3)
> >> val vals = List(0.5, 0.6)
> >> val mymap = for ( (i,v) <- ndx.zip(vals) ) yield { i -> v}
> >> // List[(Int, Double)] = List((1,0.5), (3,0.6))
>
> >> So I ended up using this ugly (but correct) imperative code:
>
> >> val mapping = mutable.Map[Int, Double]()
> >> for ( (i,v) <- ndx.zip(vals)) { mapping(i) = v }
> >> val mymap = mapping.toMap
> >> // scala.collection.immutable.Map[Int,Double] = Map(3 -> 0.6, 1 ->
> >> 0.5)
>
> >> Is there a better way?
>
> >> Tx
> >> Kevin
val mymap = ndx zip vals toMap
On Thu, Dec 15, 2011 at 9:23 PM, Kevin Murphy <murphyk2@gmail.com> wrote: