Scala 2.13:return 相同的集合类型(甚至数组和字符串)
Scala 2.13: return same collection type (even Array and String)
这是 faro shuffle 的基本实现。这是一个 out-shuffle(“faro out,伙计!”)只是因为它比 in-shuffle 更容易编码。
def faroOut[A](cards: List[A]): List[A] =
List.unfold(cards.splitAt((cards.size + 1) / 2)) {
case (a,b) => Option.when(a.nonEmpty)(a.head -> (b, a.tail))
}
faroOut(List("AS","KD","QC","JH","2S","3D","4C","5H"))
//res0: List[String] = List(AS, 2S, KD, 3D, QC, 4C, JH, 5H)
faroOut(List(1,2,3,4,5,6,7))
//res1: List[Int] = List(1, 5, 2, 6, 3, 7, 4)
这在其元素类型上是通用的,但在其集合类型上不是。让我们尝试解决这个问题。
import scala.collection.Factory
def faroOut[A, CC[x] <: Iterable[x]](cards:CC[A]
)(implicit fac: Factory[A,CC[A]]
): CC[A] =
Iterator.unfold(cards.splitAt((cards.size + 1) / 2)) {
case (a, b) => Option.when(a.nonEmpty)(a.head -> (b, a.tail))
}.to(fac)
faroOut(LazyList("AS","KD","QC","JH","2S","3D","4C","5H"))
faroOut(Vector(1,2,3,4,5,6,7))
//faroOut(Array(3,4,5,6)) <-- won't compile
将其转换为扩展方法并不太复杂,但我们在这里不必担心。
所以这适用于列表和向量,但不适用于 Array
或 String
,因为它们来自 Java-land 而不是 Scala 的一部分 Iterable
等级制度。为此,我们需要引入 IsSeq
类型 class.
有趣的是,这在 Scala-3 中非常简单。
import scala.collection.generic.IsSeq
import scala.collection.Factory
def faroOut[Repr](cards: Repr
)(using seq: IsSeq[Repr]
, fac: Factory[seq.A,Repr]): Repr =
val seqOps = seq(cards).toIterable
Iterator.unfold(seqOps.splitAt((seqOps.size + 1) / 2)) {
case (a, b) => Option.when(a.nonEmpty)(a.head -> (b, a.tail))
}.to(fac)
这里是 Scastie to prove it。
将其转换为扩展方法几乎是微不足道的,但我们在这里不必担心。
请注意 Factory[_,_]
的第一个类型参数如何依赖于参数组中的前一个参数。在 Scala-2 上不可能实现的一个很酷的 Scala-3 增强。
在 Scala docs page, and , I'm left with the QUESTION (at long last) 花了一些时间后:难道没有更小的 and/or 更简单的解决方案吗?我们 真的 需要将它变成具有所有 implicit
转换等的扩展方法吗?
要在单个参数列表限制内绕过 Scala 2 依赖类型,请尝试类型优化
IsIterable[Repr] { type A = E }
或Aux键入别名模式
type AuxA[Repr, E] = IsIterable[Repr] { type A = E }
例如
def faroOut[Repr, E](cards: Repr)(
implicit
seq: IsIterable[Repr] { type A = E },
fac: Factory[E, Repr]
): Repr = {
val seqOps = seq(cards).toIterable
Iterator.unfold(seqOps.splitAt((seqOps.size + 1) / 2)) {
case (a, b) => Option.when(a.nonEmpty)(a.head -> (b, a.tail))
}.to(fac)
}
faroOut(Array(3,4,5,6)) // : Array[Int] = Array(3, 5, 4, 6)
faroOut("ABCxyz") // : String = AxByCz
这是 faro shuffle 的基本实现。这是一个 out-shuffle(“faro out,伙计!”)只是因为它比 in-shuffle 更容易编码。
def faroOut[A](cards: List[A]): List[A] =
List.unfold(cards.splitAt((cards.size + 1) / 2)) {
case (a,b) => Option.when(a.nonEmpty)(a.head -> (b, a.tail))
}
faroOut(List("AS","KD","QC","JH","2S","3D","4C","5H"))
//res0: List[String] = List(AS, 2S, KD, 3D, QC, 4C, JH, 5H)
faroOut(List(1,2,3,4,5,6,7))
//res1: List[Int] = List(1, 5, 2, 6, 3, 7, 4)
这在其元素类型上是通用的,但在其集合类型上不是。让我们尝试解决这个问题。
import scala.collection.Factory
def faroOut[A, CC[x] <: Iterable[x]](cards:CC[A]
)(implicit fac: Factory[A,CC[A]]
): CC[A] =
Iterator.unfold(cards.splitAt((cards.size + 1) / 2)) {
case (a, b) => Option.when(a.nonEmpty)(a.head -> (b, a.tail))
}.to(fac)
faroOut(LazyList("AS","KD","QC","JH","2S","3D","4C","5H"))
faroOut(Vector(1,2,3,4,5,6,7))
//faroOut(Array(3,4,5,6)) <-- won't compile
将其转换为扩展方法并不太复杂,但我们在这里不必担心。
所以这适用于列表和向量,但不适用于 Array
或 String
,因为它们来自 Java-land 而不是 Scala 的一部分 Iterable
等级制度。为此,我们需要引入 IsSeq
类型 class.
有趣的是,这在 Scala-3 中非常简单。
import scala.collection.generic.IsSeq
import scala.collection.Factory
def faroOut[Repr](cards: Repr
)(using seq: IsSeq[Repr]
, fac: Factory[seq.A,Repr]): Repr =
val seqOps = seq(cards).toIterable
Iterator.unfold(seqOps.splitAt((seqOps.size + 1) / 2)) {
case (a, b) => Option.when(a.nonEmpty)(a.head -> (b, a.tail))
}.to(fac)
这里是 Scastie to prove it。
将其转换为扩展方法几乎是微不足道的,但我们在这里不必担心。
请注意 Factory[_,_]
的第一个类型参数如何依赖于参数组中的前一个参数。在 Scala-2 上不可能实现的一个很酷的 Scala-3 增强。
在 Scala docs page, and implicit
转换等的扩展方法吗?
要在单个参数列表限制内绕过 Scala 2 依赖类型,请尝试类型优化
IsIterable[Repr] { type A = E }
或Aux键入别名模式
type AuxA[Repr, E] = IsIterable[Repr] { type A = E }
例如
def faroOut[Repr, E](cards: Repr)(
implicit
seq: IsIterable[Repr] { type A = E },
fac: Factory[E, Repr]
): Repr = {
val seqOps = seq(cards).toIterable
Iterator.unfold(seqOps.splitAt((seqOps.size + 1) / 2)) {
case (a, b) => Option.when(a.nonEmpty)(a.head -> (b, a.tail))
}.to(fac)
}
faroOut(Array(3,4,5,6)) // : Array[Int] = Array(3, 5, 4, 6)
faroOut("ABCxyz") // : String = AxByCz