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

Creating map from list of pairs

6 replies
Dave Ray
Joined: 2009-01-07,
User offline. Last seen 42 years 45 weeks ago.

Hi. If I have a list of pairs. What's the best way to initialize a map
from them? Right now I'm doing this:

val m = Map() ++ pairs

Secondly, is there a nicer way to turn an Option into a Set than this:

def toSet[T](o : Option[T]) : Set[T] = {
o match {
case Some(some) => Set(some)
case None => Set()
}
}

Every time I write one of these utility functions I find out there's a
more elegant, built-in way to do it.

Thanks!

Dave

Bryan
Joined: 2008-12-19,
User offline. Last seen 42 years 45 weeks ago.
Re: Creating map from list of pairs
I don't know how great of a solution this is, but you could do something like: 

Map(List(Pair(1, 2), Pair(3, 4)): _*)

--Bryan

On Mon, Jan 12, 2009 at 10:59 AM, Dave Ray <daveray [at] gmail [dot] com> wrote:
Hi. If I have a list of pairs. What's the best way to initialize a map
from them? Right now I'm doing this:

  val m = Map() ++ pairs

Secondly, is there a nicer way to turn an Option into a Set than this:

 def toSet[T](o : Option[T]) : Set[T] = {
   o match {
     case Some(some) => Set(some)
     case None => Set()
   }
 }

Every time I write one of these utility functions I find out there's a
more elegant, built-in way to do it.

Thanks!

Dave

David Pollak
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Creating map from list of pairs

That's what I always do.

Thanks,

David

On Jan 12, 2009 8:25 AM, "Bryan" <germish [at] gmail [dot] com> wrote:

I don't know how great of a solution this is, but you could do something like: 

Map(List(Pair(1, 2), Pair(3, 4)): _*)

--Bryan

On Mon, Jan 12, 2009 at 10:59 AM, Dave Ray <daveray [at] gmail [dot] com> wrote: > > Hi. If I have a list of ...

ounos
Joined: 2008-12-29,
User offline. Last seen 3 years 44 weeks ago.
Re: Creating map from list of pairs

Could someone explain why the final ": _*" is needed, and what it means
exactly?

(The error if I omit it:
:4: error: type mismatch;
found : List[(Int, Int)]
required: (?, ?)
)

O/H David Pollak έγραψε:
>
> That's what I always do.
>
> Thanks,
>
> David
>
> On Jan 12, 2009 8:25 AM, "Bryan" > wrote:
>
> I don't know how great of a solution this is, but you could do
> something like:
>
> Map(List(Pair(1, 2), Pair(3, 4)): _*)
>
> --Bryan
>
> On Mon, Jan 12, 2009 at 10:59 AM, Dave Ray > wrote: > > Hi. If I have a list of ...
>

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Creating map from list of pairs
It means "Treat as vararg parameter".  You use it when you have a variable argument function, like


def varargfunc( argument : T* ) ...

On Mon, Jan 12, 2009 at 12:52 PM, Dimitris Andreou <jim [dot] andreou [at] gmail [dot] com> wrote:
Could someone explain why the final ": _*" is needed, and what it means exactly?

(The error if I omit it:
<console>:4: error: type mismatch;
found   : List[(Int, Int)]
required: (?, ?)
)

O/H David Pollak έγραψε:

That's what I always do.

Thanks,

David

   On Jan 12, 2009 8:25 AM, "Bryan" <germish [at] gmail [dot] com
   <mailto:germish [at] gmail [dot] com>> wrote:

   I don't know how great of a solution this is, but you could do
   something like:
   Map(List(Pair(1, 2), Pair(3, 4)): _*)

   --Bryan

   On Mon, Jan 12, 2009 at 10:59 AM, Dave Ray <daveray [at] gmail [dot] com
   <mailto:daveray [at] gmail [dot] com>> wrote: > > Hi. If I have a list of ...



Derek Chen-Becker
Joined: 2008-12-16,
User offline. Last seen 42 years 45 weeks ago.
Re: Creating map from list of pairs

Dimitris Andreou wrote:
> Could someone explain why the final ": _*" is needed, and what it means
> exactly?
>
> (The error if I omit it:
> :4: error: type mismatch;
> found : List[(Int, Int)]
> required: (?, ?)
> )

If I'm reading the book correctly it means to treat the argument as a
sequence of arguments (a vararg list) instead of a single argument of
type Seq[(Int, Int)].

Derek

aaronharnly
Joined: 2008-08-19,
User offline. Last seen 3 years 20 weeks ago.
Re: Creating map from list of pairs

Secondly, is there a nicer way to turn an Option into a Set than this:

def toSet[T](o : Option[T]) : Set[T] = {
o match {
case Some(some) => Set(some)
case None => Set()
}
}

 

How about this:

o.map(Set(_)).getOrElse(Set())

 

 

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