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

toMap

7 replies
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.

I threw this out on one of the mailing lists only because somebody said
it couldn't be done, but now that it's a reality, I think we should ship
it. It's useful and it closes the circle of life among the collections.

def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = {
val b = immutable.Map.newBuilder[T, U]
for (x <- this)
b += x

b.result
}

Tony Morris 2
Joined: 2009-03-20,
User offline. Last seen 42 years 45 weeks ago.
Re: toMap

Paul Phillips wrote:
> I threw this out on one of the mailing lists only because somebody said
> it couldn't be done, but now that it's a reality, I think we should ship
> it. It's useful and it closes the circle of life among the collections.
>
> def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = {
> val b = immutable.Map.newBuilder[T, U]
> for (x <- this)
> b += x
>
> b.result
> }
>
>
Is that a method on Traversable?

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: toMap
I have much agree. The Map(...: _*) and Map() ++ ... patterns are ugly and unintuitive. And, in the presence of toSeq, toList, toStream, toSet, what reason could there be for not having a toMap?

On Mon, Jan 11, 2010 at 9:32 PM, Paul Phillips <paulp@improving.org> wrote:
I threw this out on one of the mailing lists only because somebody said
it couldn't be done, but now that it's a reality, I think we should ship
it.  It's useful and it closes the circle of life among the collections.

 def toMap[T, U](implicit ev: A <:< (T, U)): immutable.Map[T, U] = {
   val b = immutable.Map.newBuilder[T, U]
   for (x <- this)
     b += x

   b.result
 }

--
Paul Phillips      | It's better to have gloved and tossed than never to
Moral Alien        | have played baseball.
Empiricist         |
i'll ship a pulp   |----------* http://www.improving.org/paulp/ *----------



--
Daniel C. Sobral

I travel to the future all the time.
extempore
Joined: 2008-12-17,
User offline. Last seen 35 weeks 3 days ago.
Re: toMap

On Tue, Jan 12, 2010 at 09:45:17AM +1000, Tony Morris wrote:
> Is that a method on Traversable?

TraversableLike actually, but same idea.

Seth Tisue
Joined: 2008-12-16,
User offline. Last seen 34 weeks 3 days ago.
Re: toMap

>>>>> "Paul" == Paul Phillips writes:

Paul> I threw this out on one of the mailing lists only because
Paul> somebody said it couldn't be done, but now that it's a reality, I
Paul> think we should ship it. It's useful and it closes the circle of
Paul> life among the collections.
Paul> def toMap [...]

Awesome. I find myself wanting this practically daily.

Viktor Klang
Joined: 2008-12-17,
User offline. Last seen 1 year 27 weeks ago.
Re: toMap
For 2.8? Please say yes.

On Tue, Jan 12, 2010 at 1:19 AM, Seth Tisue <seth@tisue.net> wrote:
>>>>> "Paul" == Paul Phillips <paulp@improving.org> writes:

 Paul> I threw this out on one of the mailing lists only because
 Paul> somebody said it couldn't be done, but now that it's a reality, I
 Paul> think we should ship it.  It's useful and it closes the circle of
 Paul> life among the collections.
 Paul>   def toMap [...]

Awesome.  I find myself wanting this practically daily.

--
Seth Tisue @ Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/



--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Blog: klangism.blogspot.com
Twttr: twitter.com/viktorklang
Code: github.com/viktorklang
Adriaan Moors
Joined: 2009-04-03,
User offline. Last seen 42 years 45 weeks ago.
Re: toMap
wouldn't this be subsumed by breakOut in most cases? see http://lampsvn.epfl.ch/trac/scala/ticket/2604#comment:6
scala> val cities = List("London", "Paris")
cities: List[java.lang.String] = List(London, Paris)

scala> import scala.collection.breakOut

scala> (cities.map(x => (x.length, x))(breakOut): Map[Int, String])
cheersadriaan
On Tue, Jan 12, 2010 at 9:23 AM, Viktor Klang <viktor.klang@gmail.com> wrote:
For 2.8? Please say yes.

On Tue, Jan 12, 2010 at 1:19 AM, Seth Tisue <seth@tisue.net> wrote:
>>>>> "Paul" == Paul Phillips <paulp@improving.org> writes:

 Paul> I threw this out on one of the mailing lists only because
 Paul> somebody said it couldn't be done, but now that it's a reality, I
 Paul> think we should ship it.  It's useful and it closes the circle of
 Paul> life among the collections.
 Paul>   def toMap [...]

Awesome.  I find myself wanting this practically daily.

--
Seth Tisue @ Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/



--
Viktor Klang
| "A complex system that works is invariably
| found to have evolved from a simple system
| that worked." - John Gall

Blog: klangism.blogspot.com
Twttr: twitter.com/viktorklang
Code: github.com/viktorklang

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm for more information.

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: toMap

On Tue, Jan 12, 2010 at 9:31 AM, Adriaan Moors
wrote:
> wouldn't this be subsumed by breakOut in most cases?

Yes, but toMap is simpler and more symmetrical. I am in favor of including it.

Cheers

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