Reputation: 2866
consider a simple function that operates on collections distinctBy
, which, like distinct
remove "duplicates" (which are not necessary actual duplicates):
import scala.collection.TraversableLike
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.{Set=>MSet}
def distinctBy[T,R,Coll]
(xs: Coll)
(f: T => R)
(implicit ev: Coll <:< TraversableLike[T,Coll],
cbf: CanBuildFrom[Coll,T,Coll]): Coll = {
val builder = cbf(xs)
builder.sizeHint(xs.size)
val seen = MSet.empty[R]
xs.foreach { elem =>
if(!seen(f(elem))){
builder += elem
}
}
builder.result()
}
now consider a class to use it on:
case class X(i: Int, j: Int)
using this function naively fails:
scala> distinctBy(Vector(X(1,2),X(3,2),X(1,1),X(2,2)))(_.i)
<console>:14: error: missing parameter type for expanded function ((x$1) => x$1.i)
distinctBy(Vector(X(1,2),X(3,2),X(1,1),X(2,2)))(_.i)
^
<console>:14: error: Cannot construct a collection of type scala.collection.immutable.Vector[X] with elements of type Any based on a collection of type scala.collection.immutable.Vector[X].
distinctBy(Vector(X(1,2),X(3,2),X(1,1),X(2,2)))(_.i)
^
but if I help the type inferencer, this works:
scala> distinctBy(Vector(X(1,2),X(3,2),X(1,1),X(2,2)))((x:X) => x.i)
res1: scala.collection.immutable.Vector[X] = Vector(X(1,2), X(3,2), X(1,1), X(2,2))
scala> distinctBy[X,Int,Vector[X]](Vector(X(1,2),X(3,2),X(1,1),X(2,2)))(_.i)
res2: scala.collection.immutable.Vector[X] = Vector(X(1,2), X(3,2), X(1,1), X(2,2))
to my best understanding, since the function is given in a second argument list, the type inferencer should have picked up that it's a function from X
to something. and since X
has a member i
of type Int
, all should have been OK with the first try. so, what am I missing here?
Upvotes: 1
Views: 72
Reputation: 6385
This simplified version works fine for me:
object A {
def f1[T, R](l: List[T])(f: T=>R) = None
case class X(i: Int, j: Int)
f1(List(X(1,1),X(2,1)))(_.i)
}
As you can see collection in first parameter list has T type that allows scala inference type in second arguments list.
So you need build somehow dependencies between Col and T in your example. Not sure if third implicits parameters list helps here.
UPD. Looks weird but seems it works:
object A {
def f1[T, R, Col[Z]](l: Col[T])(f: T => R) = None
case class X(i: Int, j: Int)
f1(List(X(1,1),X(2,1)))(_.i)
}
UPD2. Rewritten sample from question.
import scala.collection.TraversableLike
import scala.collection.generic.CanBuildFrom
import scala.collection.mutable.{Set=>MSet}
def distinctBy[T,R,Coll[Z]]
(xs: Coll[T])
(f: T => R)
(implicit ev: Coll[T] <:< TraversableLike[T,Coll[T]],
cbf: CanBuildFrom[Coll[T],T,Coll[T]]): Coll[T] = {
val builder = cbf(xs)
builder.sizeHint(xs.size)
val seen = MSet.empty[R]
xs.foreach { elem =>
if(!seen(f(elem))){
builder += elem
seen.add(f(elem))
}
}
builder.result()
}
case class X(i: Int, j: Int)
distinctBy(Vector(X(1,2),X(1,2),X(3,2),X(1,1),X(2,2)))(_.i)
distinctBy(Map("1" -> X(1,2), "2" -> X(1,2), "3" -> X(3,2)))(_._2.i)
Upvotes: 2