从函数参数推断类型参数

时间:2015-03-25 21:59:51

标签: scala

another stackoverflow question的上下文中,我有这个片段:

def orderedGroupBy[T, P](seq: Traversable[T], f: T => P): Traversable[Tuple2[P, Traversable[T]]] = {
  @tailrec
  def accumulator(seq: Traversable[T], f: T => P, res: List[Tuple2[P, Traversable[T]]]): Traversable[Tuple2[P, Traversable[T]]] = seq.headOption match {
    case None => res.reverse
    case Some(h) => {
      val key = f(h)
      val subseq = seq.takeWhile(f(_) == key)
      accumulator(seq.drop(subseq.size), f, (key -> subseq) :: res)
    }
  }
  accumulator(seq, f, Nil)
}

我想像使用.groupBy一样使用它,例如:

orderedGroupBy(1 to 100, (_ / 10))

但编译器会因没有足够的类型信息而产生错误

<console>:10: error: missing parameter type for expanded function ((x$1) => x$1.$div(10))
              orderedGroupBy(1 to 100, (_ / 10))

这样做的惯用方法是什么?

1 个答案:

答案 0 :(得分:4)

您可以对参数进行调整,以便T仅从seq: Traversable[T]推断{/ 1}}。

def orderedGroupBy[T, P](seq: Traversable[T])(f: T => P): Traversable[Tuple2[P, Traversable[T]]] = ???

scala> orderedGroupBy(1 to 100)(_ / 10)
res110: Traversable[(Int, Traversable[Int])] = List((0,Range(1, 2, 3, 4, 5, 6, 7, 8, 9)), (1,Range(10, 11, 12, 13, 14, 15, 16, 17, 18, 19)), (2,Range(20, 21, 22, 23, 24, 25, 26, 27, 28, 29)), (3,Range(30, 31, 32, 33, 34, 35, 36, 37, 38, 39)), (4,Range(40, 41, 42, 43, 44, 45, 46, 47, 48, 49)), (5,Range(50, 51, 52, 53, 54, 55, 56, 57, 58, 59)), (6,Range(60, 61, 62, 63, 64, 65, 66, 67, 68, 69)), (7,Range(70, 71, 72, 73, 74, 75, 76, 77, 78, 79)), (8,Range(80, 81, 82, 83, 84, 85, 86, 87, 88, 89)), (9,Range(90, 91, 92, 93, 94, 95, 96, 97, 98, 99)), (10,Range(100)))