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

How to pimp Scala collections with my own generic `map` (the right way)?

14 replies
Yang Zhang
Joined: 2010-02-09,
User offline. Last seen 42 years 45 weeks ago.

This was originally posted to SO - any hints?

http://stackoverflow.com/questions/8472654/how-to-pimp-scala-collections...

I think dcsobral's answer comes close but we can't figure out how to
get it to work with implicits.

Also, would hugely appreciate anything in the way of English explanations.

Yang Zhang
Joined: 2010-02-09,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (the

(By "get it to work with implicits" I mean "get it to work with
Arrays, Strings, and other types that can be viewed as collections.)

On Mon, Dec 12, 2011 at 6:22 PM, Yang Zhang wrote:
> This was originally posted to SO - any hints?
>
> http://stackoverflow.com/questions/8472654/how-to-pimp-scala-collections...
>
> I think dcsobral's answer comes close but we can't figure out how to
> get it to work with implicits.
>
> Also, would hugely appreciate anything in the way of English explanations.

kmels
Joined: 2010-02-11,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (th
Yang Zhang
Joined: 2010-02-09,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (th

2011/12/12 Carlos López Camey :
> 2011/12/12 Yang Zhang :
>> This was originally posted to SO - any hints?
>>
>> http://stackoverflow.com/questions/8472654/how-to-pimp-scala-collections...
>>
>
> This might help:
>
> http://stackoverflow.com/questions/3225675/can-i-pimp-my-library-with-an...

retronym's answer too falls short, and it furthermore assumes a higher
kinded type.

scala> Array(1).mapmap(1+)
:15: error: value mapmap is not a member of Array[Int]
Array(1).mapmap(1+)
^

scala> "hello".mapmap(x=>x)
:15: error: value mapmap is not a member of java.lang.String
"hello".mapmap(x=>x)
^

Derek Williams 3
Joined: 2011-08-12,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (th
2011/12/12 Yang Zhang <yanghatespam@gmail.com>
retronym's answer too falls short, and it furthermore assumes a higher
kinded type.

scala> Array(1).mapmap(1+)
<console>:15: error: value mapmap is not a member of Array[Int]
             Array(1).mapmap(1+)
                      ^

scala> "hello".mapmap(x=>x)
<console>:15: error: value mapmap is not a member of java.lang.String
             "hello".mapmap(x=>x)
                     ^


I haven't tested it, but have you tried using '<%' instead of '<:'?
implicit def conv[A,C <% GenTraversable[A]](xs: C with GenTraversableLike[A,C])
--
Derek Williams
Yang Zhang
Joined: 2010-02-09,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (th

On Mon, Dec 12, 2011 at 7:40 PM, Derek Williams wrote:
> 2011/12/12 Yang Zhang
>>
>> retronym's answer too falls short, and it furthermore assumes a higher
>> kinded type.
>>
>> scala> Array(1).mapmap(1+)
>> :15: error: value mapmap is not a member of Array[Int]
>>              Array(1).mapmap(1+)
>>                       ^
>>
>> scala> "hello".mapmap(x=>x)
>> :15: error: value mapmap is not a member of java.lang.String
>>              "hello".mapmap(x=>x)
>>                      ^
>>
>
> I haven't tested it, but have you tried using '<%' instead of '<:'?
>
> implicit def conv[A,C <% GenTraversable[A]](xs: C with
> GenTraversableLike[A,C])

That doesn't work either.

Yang Zhang
Joined: 2010-02-09,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (th

On Mon, Dec 12, 2011 at 7:52 PM, Yang Zhang wrote:
> On Mon, Dec 12, 2011 at 7:40 PM, Derek Williams wrote:
>> 2011/12/12 Yang Zhang
>>>
>>> retronym's answer too falls short, and it furthermore assumes a higher
>>> kinded type.
>>>
>>> scala> Array(1).mapmap(1+)
>>> :15: error: value mapmap is not a member of Array[Int]
>>>              Array(1).mapmap(1+)
>>>                       ^
>>>
>>> scala> "hello".mapmap(x=>x)
>>> :15: error: value mapmap is not a member of java.lang.String
>>>              "hello".mapmap(x=>x)
>>>                      ^
>>>
>>
>> I haven't tested it, but have you tried using '<%' instead of '<:'?
>>
>> implicit def conv[A,C <% GenTraversable[A]](xs: C with
>> GenTraversableLike[A,C])
>
> That doesn't work either.

Guess I might as well paste:

scala> implicit def conv[A,C <% GenTraversable[A]](xs: C with
GenTraversableLike[A,C])
| = new { def mymap[B,D](f: A => B)(implicit b:
CanBuildFrom[C,B,D]): D = b(xs).result // placeholder
| }
conv: [A, C](xs: C with
scala.collection.GenTraversableLike[A,C])(implicit evidence$1: C =>
scala.collection.GenTraversable[A])java.lang.Object{def mymap[B,D](f:
A => B)(implicit b: scala.collection.generic.CanBuildFrom[C,B,D]): D}

scala> List(1) mymap (_+1)
res0: List[Int] = List()

scala> Array(1) mymap (_+1)
:15: error: value mymap is not a member of Array[Int]
Array(1) mymap (_+1)
^

Stefan Wagner
Joined: 2011-04-08,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (th

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Am 13.12.2011 03:22, schrieb Yang Zhang:

We don't "pimp" any more, we "enrich" now. ;)

Yang Zhang
Joined: 2010-02-09,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (th

On Mon, Dec 12, 2011 at 8:58 PM, Stefan Wagner wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Am 13.12.2011 03:22, schrieb Yang Zhang:
>
> We don't "pimp" any more, we "enrich" now. ;)

Duly noted. :)

Derek Williams 3
Joined: 2011-08-12,
User offline. Last seen 42 years 45 weeks ago.
Re: How to pimp Scala collections with my own generic `map` (th
Here is something that almost works:
implicit def conv[A, C[_]](xs: C[A])(implicit ev: C[A] => GenTraversable[A]) = new {  def mymap[B,D](f: A => B)(implicit cbf: CanBuildFrom[C[A],B,D]): D = {     ...  }}
It works with Arrays, but it doesn't work with Strings.
--
Derek Williams
Lars Hupel
Joined: 2010-06-23,
User offline. Last seen 44 weeks 3 days ago.
Re: How to pimp Scala collections with my own generic `map` (the

Here's another version, which doesn't work as an implicit, but I have no
idea why:

implicit def conv[A, Coll]
(xs: Coll)(implicit ev: Coll => TraversableLike[A, Coll]) = new {

def foo[B, That]
(f: A => B)(implicit cbf: CanBuildFrom[Coll, B, That]) = null

}

scala> Array(1,2,3).foo(_.toString)
:15: error: No implicit view available from Array[Int] =>
scala.collection.TraversableLike[A,Array[Int]].
Array(1,2,3).foo(_.toString)

However:

scala> conv(Array(1,2,3)).foo(_.toString)
res1: Null = null

Yang Zhang
Joined: 2010-02-09,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: How to pimp Scala collections with my own generic `map`

On Tue, Dec 13, 2011 at 12:21 AM, Lars Hupel wrote:
> Here's another version, which doesn't work as an implicit, but I have no
> idea why:
>
> implicit def conv[A, Coll]
>  (xs: Coll)(implicit ev: Coll => TraversableLike[A, Coll]) = new {
>
>    def foo[B, That]
>      (f: A => B)(implicit cbf: CanBuildFrom[Coll, B, That]) = null
>
> }
>
> scala> Array(1,2,3).foo(_.toString)
> :15: error: No implicit view available from Array[Int] =>
> scala.collection.TraversableLike[A,Array[Int]].
>              Array(1,2,3).foo(_.toString)
>
>
> However:
>
> scala> conv(Array(1,2,3)).foo(_.toString)
> res1: Null = null
>

This one was also in my original SO question :)

Lars Hupel
Joined: 2010-06-23,
User offline. Last seen 44 weeks 3 days ago.
Re: How to pimp Scala collections with my own generic `map` (the

> This one was also in my original SO question :)

Yours were slightly different -- the first one used `<:<` instead of
`=>` which doesn't work if you have an implicit conversion; and the
second one used `GenTraversable` which doesn't work for non-generic
collections. But yes, it suffers from exactly the same problems.

Stefan Zeiger
Joined: 2008-12-21,
User offline. Last seen 27 weeks 4 days ago.
Re: Re: How to pimp Scala collections with my own generic `map`

On 2011-12-13 9:21, Lars Hupel wrote:
> Here's another version, which doesn't work as an implicit, but I have no
> idea why:

Probably due to https://issues.scala-lang.org/browse/SI-3346

-sz

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: How to pimp Scala collections with my own generic `map` (th

On Tue, Dec 13, 2011 at 00:22, Yang Zhang wrote:
> This was originally posted to SO - any hints?
>
> http://stackoverflow.com/questions/8472654/how-to-pimp-scala-collections...
>
> I think dcsobral's answer comes close but we can't figure out how to
> get it to work with implicits.
>
> Also, would hugely appreciate anything in the way of English explanations.

Yang, at the time I tried to find a link without success, but I just
found it: https://gist.github.com/257758

This shows type bounds, and explains a bit the difference between
them. It won't help you with what you want, but you had questions
about this too. :-)

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