3

注意-下面描述的操作现在存在于标准库中,partitionMap但我相信关于如何实现更一般的目标仍然是一个有效的问题

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

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

按照文档,我设法实现了以下目标:

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))
4

2 回答 2

4

这似乎有效:

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)
于 2019-06-11T12:18:48.163 回答
3

In your case you don’t want to abstract over the “kind” of the collection type constructor (CC[_] vs CC[_, _], etc.), you always use the CC[_] kind, so you don’t need to use IsIterable.

I think it is also not necessary to support “Sorted” collections (eg, SortedSet) because there is no Ordering instance for Either, so you don’t need to use 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

于 2019-10-18T08:25:22.013 回答