在Scala 2.13中将自定义集合操作添加到特定类型的任意集合

时间:2019-06-11 11:06:48

标签: scala scala-2.13

注意-下面描述的操作现在在标准库中以partitionMap的形式存在,但我认为对于如何实现更一般的目的这仍然是一个有效的问题

有关scala 2.13的问题-在需要限制输入集合的元素类型的自定义集合操作中,如何使用/构造特定类型的集合?例如我该如何定义:

def split[CC[_], A, B](coll: CC[Either[A, B]]): (CC[A], CC[B])

documentation之后,我设法做到了以下几点:

import collection.generic.IsIterable
import scala.collection.{BuildFrom, Factory}

class SplitOperation[Repr, S <: IsIterable[Repr]](coll: Repr, itr: S) {
  def split[A, B, AS, BS](
    implicit bfa: BuildFrom[Repr, A, AS], 
             bfb: BuildFrom[Repr, B, BS], 
             ev: itr.A =:= Either[A, B]): (AS, BS) = {

    val ops = itr(coll)
    val as = bfa.fromSpecific(coll)(ops.iterator.map(ev).collect { case Left(a) => a })
    val bs = bfb.fromSpecific(coll)(ops.iterator.map(ev).collect { case Right(b) => b })

    (as, bs)
  }
}

implicit def SplitOperation[Repr](coll: Repr)(implicit itr: IsIterable[Repr]): SplitOperation[Repr, itr.type] =
new SplitOperation(coll, itr)

但是,我需要在使用现场提供类型,否则我将产生不同的隐式扩展。

scala> List(Left("bah"), Right(1), Left("gah"), Right(2), Right(3))
res1: List[scala.util.Either[String,Int]] = List(Left(bah), Right(1), Left(gah), Right(2), Right(3))

scala> res1.split
            ^
       error: diverging implicit expansion for type scala.collection.BuildFrom[List[scala.util.Either[String,Int]],A,AS]

但是以下方法可行:

scala> res1.split[String, Int, List[String], List[Int]]
res4: (List[String], List[Int]) = (List(bah, gah),List(1, 2, 3))

编辑

class SplitOperation[X, CC[_], S <: IsIterable[CC[X]]](coll: CC[X], itr: S) {
  def split[A, B](implicit bfa: BuildFrom[CC[X], A, CC[A]], bfb: BuildFrom[CC[X], B, CC[B]], ev: itr.A =:= Either[A, B]): (CC[A], CC[B]) = {
    val ops = itr(coll)
    val as = bfa.fromSpecific(coll)(ops.iterator.map(ev).collect { case Left(a) => a })
    val bs = bfb.fromSpecific(coll)(ops.iterator.map(ev).collect { case Right(b) => b })

    (as, bs)
  }
}

implicit def SplitOperation[A, B, CC[_]](coll: CC[Either[A, B]])(implicit itr: IsIterable[CC[Either[A, B]]]): SplitOperation[Either[A, B], CC, itr.type] =
  new SplitOperation(coll, itr)

给我一​​点改善。现在,我只需要在呼叫站点提供类型参数AB

scala> l.split[String, Int]
res2: (List[String], List[Int]) = (List(bah, gah),List(1, 2))

2 个答案:

答案 0 :(得分:2)

如果您不想抽象集合类型构造函数的“种类”(CC[_]CC[_, _]等),请始终使用CC[_]类型,因此您无需使用IsIterable

我认为也没有必要支持“排序的”集合(例如SortedSet),因为Ordering没有Either实例,因此您无需使用BuildFrom

implicit class SplitOperation[A, B, CC[X] <: IterableOps[X, CC, CC[X]]](coll: CC[Either[A, B]]) {
  def split: (CC[A], CC[B]) = {
    val as = coll.iterableFactory.from(coll.iterator.collect { case Left(a) => a })
    val bs = coll.iterableFactory.from(coll.iterator.collect { case Right(b) => b })
    (as, bs)
  }
}

https://scastie.scala-lang.org/64QxHwteQN2i3udSxCa3yw

答案 1 :(得分:1)

这似乎可行:

class SplitOperation[A, B, CC[_], S <: IsIterable[CC[Either[A, B]]]](coll: CC[Either[A, B]], itr: S) {
  def split(implicit bfa: BuildFrom[CC[Either[A, B]], A, CC[A]], bfb: BuildFrom[CC[Either[A, B]], B, CC[B]], ev: itr.A =:= Either[A, B]): (CC[A], CC[B]) = {
    val ops = itr(coll)
    val as = bfa.fromSpecific(coll)(ops.iterator.map(ev).collect { case Left(a) => a })
    val bs = bfb.fromSpecific(coll)(ops.iterator.map(ev).collect { case Right(b) => b })

    (as, bs)
  }
}

implicit def SplitOperation[A, B, CC[_]](coll: CC[Either[A, B]])(implicit itr: IsIterable[CC[Either[A, B]]]): SplitOperation[A, B, CC, itr.type] =
  new SplitOperation(coll, itr)