You can't do this for all Traversables, as they don't guarantee that map returns anything more specific than Traversable See Update 2 below import collection.generic. CanBuildFrom import collection. TraversableLike class TraversableWCCX A)(implicit cbf: CanBuildFromCCA, A, CCA): CCA = value.
Map(f andThen f) def mapToString(implicit cbf: CanBuildFromCCA, String, CCString): CCString = value. Map(_. ToString) } object TraversableW { implicit def TraversableWToCCX A)(implicit cbf: CanBuildToA, CC): CCA = value.
MapA, CCA(f andThen f)(collection. BreakOut) def mapToString(implicit cbf: CanBuildToString, CC): CCString = value. MapString, CCString(_.
ToString)(collection. BreakOut) } object TraversableW { implicit def TraversableWToCCX B)(implicit bf: CanBuildFromRepr, B, That): That = { val be = bf(repr) b. Sizent(this) for (x Result } The Builder be is initialized with the original collection, which is the mechanism to preserve the dynamic type through a map However, our CanBuildFrom disavowed all knowledge of the From by way of the type argument Nothing All you can do with Nothing is ignore it, which is exactly what breakOut does: def breakOutFrom, T, To(implicit be : CanBuildFromNothing, T, To) = new CanBuildFromFrom, T, To { def apply(from: From) = b.apply(); def apply() = b.apply() } We can't call b.
Apply(from) no more than you could call def foo(a: Nothing) = 0.
You can't do this for all Traversables, as they don't guarantee that map returns anything more specific than Traversable. See Update 2 below. Import collection.generic.
CanBuildFrom import collection. TraversableLike class TraversableWCCX A)(implicit cbf: CanBuildFromCCA, A, CCA): CCA = value. Map(f andThen f) def mapToString(implicit cbf: CanBuildFromCCA, String, CCString): CCString = value.
Map(_. ToString) } object TraversableW { implicit def TraversableWToCCX A)(implicit cbf: CanBuildToA, CC): CCA = value. MapA, CCA(f andThen f)(collection.
BreakOut) def mapToString(implicit cbf: CanBuildToString, CC): CCString = value. MapString, CCString(_. ToString)(collection.
BreakOut) } object TraversableW { implicit def TraversableWToCCX B)(implicit bf: CanBuildFromRepr, B, That): That = { val be = bf(repr) b. Sizent(this) for (x However, our CanBuildFrom disavowed all knowledge of the From, by way of the type argument Nothing. All you can do with Nothing is ignore it, which is exactly what breakOut does: def breakOutFrom, T, To(implicit be : CanBuildFromNothing, T, To) = new CanBuildFromFrom, T, To { def apply(from: From) = b.apply(); def apply() = b.apply() } We can't call b.
Apply(from), no more than you could call def foo(a: Nothing) = 0.
Works perfectly! – Scott Morrison Jul 12 '10 at 5:48 2 I took a slightly different approach in Scalaz, which is a bit more powerful: github. Com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/… github.Com/scalaz/scalaz/blob/master/core/src/main/scala/scalaz/… – retronym Jul 12 '10 at 6:09 I don't have the rights to edit it, retronym, but perhaps the formatted of the code block should be extended to include the imports in your example?
Cheers! – pr1001 Jul 12 '10 at 7:05 Thanks, I've fixed the formatting. – retronym Jul 12 '10 at 8:45 btw: what does "locally" do?
Googling "scala locally" doesn't seem to find an explanation. – Scott Morrison Jul 12 '10 at 17:12.
As a general rule, when you want to return objects with the same type, you need TraversableLike (IterableLike, SeqLike, etc. ) instead of Traversable. Here is the most general version I could come up with (the separate FancyTraversable class is there to avoid inferring structural types and the reflection hit): class FancyTraversableA, S A)(implicit bf: CanBuildFromS,A,S): S = { t map { a: A => f(f(a)) } } } implicit def createFancyTraversableA, S.
I'm getting an "error: not found: type TraversableLike". (2.8.0RC7) – Scott Morrison Jul 12 '10 at 5:37 "import scala.collection. _" and "import scala.collection.generic.
_" at least makes it compile, but now "List(1,2,3) mapmap { _ + 1 }" just gives me "error: value mapmap is not a member of ListInt". – Scott Morrison Jul 12 '10 at 5:45.
You can't do this for all Traversables, as they don't guarantee that map returns anything more specific than Traversable. See Update 2 below. I've added another pimped method, mapToString, to demonstrate why TraversableW accepts two type parameters, rather than one parameter as in Alexey's solution.
The parameter CC is a higher kinded type, it represents the container type of the original collection. The second parameter, A, represents the element type of the original collection. The method mapToString is thus able to return the original container type with a different element type: CCString.
Thanks to @oxbow_lakes comment, I've rethought this. It is indeed possible to directly pimp CCX We had to use collection. BreakOut, because we can't recover the specific collection subtype from a mere TraversableA.
The Builder be is initialized with the original collection, which is the mechanism to preserve the dynamic type through a map. However, our CanBuildFrom disavowed all knowledge of the From, by way of the type argument Nothing. We can't call b.
Apply(from), no more than you could call def foo(a: Nothing) = 0.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.