【发布时间】:2015-03-25 21:59:51
【问题描述】:
在another stackoverflow question的上下文中,我有这个sn-p:
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))
这样做的惯用方法是什么?
【问题讨论】:
标签: scala