簡體   English   中英

在Scala 2.13中將自定義集合操作添加到特定類型的任意集合

[英]Adding custom collection operations in scala 2.13 to arbitrary collections of specific types

注意 - 下面描述的操作現在在標准庫中作為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))

這似乎可行:

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)

在您的情況下,您不想抽象集合類型構造函數的“種類”( CC[_]CC[_, _]等),而是始終使用CC[_]類型,因此您不不需要使用IsIterable

我認為也沒有必要支持“排序的”集合(例如SortedSet ),因為Either沒有Ordering實例,因此您不需要使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM