- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
toMap
Tue, 2010-01-12, 00:32
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
}
Tue, 2010-01-12, 00:57
#2
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:
--
Daniel C. Sobral
I travel to the future all the time.
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.
Tue, 2010-01-12, 01:07
#3
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.
Tue, 2010-01-12, 01:27
#4
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.
Tue, 2010-01-12, 09:27
#5
Re: toMap
For 2.8? Please say yes.
On Tue, Jan 12, 2010 at 1:19 AM, Seth Tisue <seth@tisue.net> wrote:
--
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
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
Tue, 2010-01-12, 09:37
#6
Re: toMap
wouldn't this be subsumed by breakOut in most cases? see http://lampsvn.epfl.ch/trac/scala/ticket/2604#comment:6
On Tue, Jan 12, 2010 at 9:23 AM, Viktor Klang <viktor.klang@gmail.com> wrote:
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.
Tue, 2010-01-12, 11:07
#7
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
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?