例如:
List(5, 2, 3, 3, 3, 5, 5, 3, 3, 2, 2, 2)
我想去:
List(List(5), List(2), List(3, 3, 3), List(5, 5), List(3, 3), List(2, 2, 2))
我认为有一个简单的List函数可以执行此操作,但无法找到它。
答案 0 :(得分:16)
这是我通常使用的技巧:
def split[T](list: List[T]) : List[List[T]] = list match {
case Nil => Nil
case h::t => val segment = list takeWhile {h ==}
segment :: split(list drop segment.length)
}
实际上......不是,我通常会对集合类型进行抽象,并使用尾递归进行优化,但希望保持答案简单。
答案 1 :(得分:8)
val xs = List(5, 2, 3, 3, 3, 5, 5, 3, 3, 2, 2, 2)
这是另一种方式。
(List(xs.take(1)) /: xs.tail)((l,r) =>
if (l.head.head==r) (r :: l.head) :: l.tail else List(r) :: l
).reverseMap(_.reverse)
答案 2 :(得分:8)
Damn Rex Kerr,写下我想要的答案。由于存在轻微的风格差异,这是我的看法:
list.tail.foldLeft(List(list take 1)) {
case (acc @ (lst @ hd :: _) :: tl, el) =>
if (el == hd) (el :: lst) :: tl
else (el :: Nil) :: acc
}
由于元素相同,我没有打扰逆转子列表。
答案 3 :(得分:6)
list.foldRight(List[List[Int]]()){
(e, l) => l match {
case (`e` :: xs) :: fs => (e :: e :: xs) :: fs
case _ => List(e) :: l
}
}
或者
list.zip(false :: list.sliding(2).collect{case List(a,b) => a == b}.toList)
.foldLeft(List[List[Int]]())((l,e) => if(e._2) (e._1 :: l.head) :: l.tail
else List(e._1) :: l ).reverse
<强> [编辑] 强>
//find the hidden way
//the beauty must be somewhere
//when we talk scala
def split(l: List[Int]): List[List[Int]] =
l.headOption.map{x => val (h,t)=l.span{x==}; h::split(t)}.getOrElse(Nil)
答案 4 :(得分:5)
我在处理集合方法时有这些实现。最后,我检查了更简单的inits和tails实现并省略了集群。无论多么简单,每种新方法最终都会收取很难从外部看到的大税。但这是我没有使用的实现。
import generic._
import scala.reflect.ClassManifest
import mutable.ListBuffer
import annotation.tailrec
import annotation.unchecked.{ uncheckedVariance => uV }
def inits: List[Repr] = repSequence(x => (x, x.init), Nil)
def tails: List[Repr] = repSequence(x => (x, x.tail), Nil)
def cluster[A1 >: A : Equiv]: List[Repr] =
repSequence(x => x.span(y => implicitly[Equiv[A1]].equiv(y, x.head)))
private def repSequence(
f: Traversable[A @uV] => (Traversable[A @uV], Traversable[A @uV]),
extras: Traversable[A @uV]*): List[Repr] = {
def mkRepr(xs: Traversable[A @uV]): Repr = newBuilder ++= xs result
val bb = new ListBuffer[Repr]
@tailrec def loop(xs: Repr): List[Repr] = {
val seq = toCollection(xs)
if (seq.isEmpty)
return (bb ++= (extras map mkRepr)).result
val (hd, tl) = f(seq)
bb += mkRepr(hd)
loop(mkRepr(tl))
}
loop(self.repr)
}
<编辑:我忘了其他人不会知道内部人员。此代码是从TraversableLike内部编写的,因此它不会开箱即用。]
答案 5 :(得分:2)
这是@Kevin Wright和@Landei启发的尾递归解决方案:
@tailrec
def sliceEqual[A](s: Seq[A], acc: Seq[Seq[A]] = Seq()): Seq[Seq[A]] = {
s match {
case fst :: rest =>
val (l, r) = s.span(fst==)
sliceEqual(r, acc :+ l)
case Nil => acc
}
}
答案 6 :(得分:0)
这里稍微干净一点:
const sum = (function() {
return function sum(...args) {
return args.reduce((a, b) => a + b, 0);
};
})(); //what I am asking is (func....)(); what is the reason behind those 2 ()?
console.log(sum(1, 2, 3, 4));
尾递归版本
def groupConsequtive[A](list: List[A]): List[List[A]] = list match {
case head :: tail =>
val (t1, t2) = tail.span(_ == head)
(head :: t1) :: groupConsequtive(t2)
case _ => Nil
}
答案 7 :(得分:-1)
这可能更简单:
val input = List(5, 2, 3, 3, 3, 5, 5, 3, 3, 2, 2, 2)
input groupBy identity values