Write An Generic Function可以将Array [Array [T]]或List [List [T]]或List [Array [T]]或SeqLike [SeqLike [T]]作为Scala中的参数

时间:2017-02-27 14:27:52

标签: scala generics

如何编写函数可以将Array [Array [T]]或List [List [T]]或List [Array [T]]或SeqLike [SeqLike [T]]作为参数?

有些功能如下:

def transposeMatrix[T, S <% Seq[T], SS <% Seq[S]](xss: SS): Seq[Seq[T]] = for (i <- xss.head.indices) yield for (xs <- xss) yield xs(i) val v = transposeMatrix(matrix)

矩阵是List [Array [String]],但矩阵可以是任何数组[Array [T]]或List [List [T]]或List [Array [T]]或SeqLike [SeqLike [T]] < / p>

或以其他方式,与{1}} scala库的transpose特征非常相似

BTW:现在我收到错误“错误:(48,26)类型不匹配;  找到了

GenericTraversableTemplate

)“在跑步期间

1 个答案:

答案 0 :(得分:0)

我猜你可以使用更高的类型,并使用下限来获取你想要的语义。您也可以通过(function(document) { ... }(document)); 明确地将higherKinds传递给build.sbt,在-language:higherKinds中启用scalacOptions

import scala.language.higherKinds

def test[
  M[X] <: TraversableOnce[X],
  Inner[Y] <: TraversableOnce[Y],
  T
](col: M[Inner[T]): Bla