Creating map from list of pairs

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

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())

 

 

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

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 ...

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 ...
>

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

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 ...



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