- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
How to pimp Scala collections with my own generic `map` (the right way)?
Tue, 2011-12-13, 03:22
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.
Tue, 2011-12-13, 04:11
#2
Re: How to pimp Scala collections with my own generic `map` (th
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...
Tue, 2011-12-13, 04:21
#3
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)
^
Tue, 2011-12-13, 04:51
#4
Re: How to pimp Scala collections with my own generic `map` (th
2011/12/12 Yang Zhang <yanghatespam@gmail.com>
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
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
Tue, 2011-12-13, 05:01
#5
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.
Tue, 2011-12-13, 05:11
#6
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)
^
Tue, 2011-12-13, 06:01
#7
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. ;)
Tue, 2011-12-13, 06:51
#8
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. :)
Tue, 2011-12-13, 07:01
#9
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
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
Tue, 2011-12-13, 09:31
#10
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
Tue, 2011-12-13, 09:41
#11
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 :)
Tue, 2011-12-13, 09:51
#12
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.
Tue, 2011-12-13, 11:41
#13
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
Tue, 2011-12-13, 17:51
#14
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. :-)
(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.