Scala:查找列表中的所有链

时间:2015-11-05 08:07:47

标签: list scala functional-programming sequence fold

说我有一个项目列表:

Seq(A, B, B, B, B, G, G, S, S, S, B, A, G)

我想找到所有的链并得到它们的序列如下:

Seq(Seq(A), Seq(B, B, B, B), Seq(G, G), Seq(S, S, S), Seq(B), Seq(A), Seq(G))

我想维护顺序,并使用自定义比较函数来确定两个对象是否“相同”。我认为折叠或扫描可能是我需要的,但是我遇到了确切的情况。我正在使用Scala。

编辑:我已经修改了类似问题的答案,以便得到这个:

def collapse(input: Seq[Stmt]): Seq[Seq[Stmt]] = {
    val (l, r) = input.span(_.getClass == input.head.getClass)
    l :: collapse(r)
}

4 个答案:

答案 0 :(得分:2)

清洁解决方案:

  def pack[T](input: List[T]): List[List[T]] =
    input.foldRight(Nil : List[List[T]]) ((e, accu) => accu match {
      case Nil => List(List(e))
      case curList@(h :: t) if e == h => List(e) :: curList
      case curList@(h :: t) => List(List(e)) ::: curList
  })

不使用任何库函数(丑陋):

  def pack[T](input: List[T]): List[List[T]] = {
    def packWithPrevious(remaining: List[T])(previous: List[T]): List[List[T]] =
      remaining match {
        case List() => List(previous)
        case head :: tail =>
          val nextIter = packWithPrevious(tail)(_)
          previous match {
            case List() => nextIter(List(head))
            case prevHead :: _ =>
              if (head != prevHead)
                previous :: nextIter(List(head))
              else
                nextIter(head :: previous)
          }
      }
    packWithPrevious(input)(List())
  }

scala> val s = List('A', 'B', 'B', 'B', 'B', 'G', 'G', 'S', 'S', 'S', 'B', 'A', 'G')
s: List[Char] = List(A, B, B, B, B, G, G, S, S, S, B, A, G)

scala> pack(s)
res2: List[List[Char]] = List(List(A), List(B, B, B, B), List(G, G), List(S, S, S), List(B), List(A), List(G))

来源:https://github.com/izmailoff/scala-s-99/blob/master/src/main/scala/s99/p09/P09.scala

测试:https://github.com/izmailoff/scala-s-99/blob/master/src/test/scala/s99/p09/P09Suite.scala

答案 1 :(得分:1)

与现有答案类似,但我发现在foldLeft中直接使用部分功能作为一个干净的解决方案:

val s = Seq("A", "B", "B", "B", "B", "G", "G", "S", "S", "S", "B", "A", "G")

s.foldLeft(Seq[Seq[String]]()) {
  case (Seq(), item) => Seq(Seq(item))
  case (head::tail, item) if head.contains(item) => (item +: head) +: tail
  case (seq, item) => Seq(item) +: seq
}.reverse

res0: Seq[Seq[String]] = List(List(A), List(B, B, B, B), List(G, G), List(S, S, S), List(B), List(A), List(G))

答案 2 :(得分:0)

考虑以下解决方案:

public void freeGoldOrNot(){

    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();

       //Start this after the entire mInterstitialAd is shown
       // if not, do not call
       beginPlayingGame();

    else{
       Toast.makeText(getApplicationContext(), "Try again!",   Toast.LENGTH_SHORT).show();

    }

}

seq可能为空,所以:

seq.foldLeft(List(List(seq.head))) { case (acc,item)=> 
  if(acc.head.head==item) (item::acc.head)::acc.tail else List(item)::acc  
}.reverse

答案 3 :(得分:0)

我认为groupBy在这里会有所帮助,但我的解决方案有点尴尬:

val seq = Seq("A", "B", "B", "B", "B", "G", "G", "S", "S", "S", "B", "A", "G")
val parts = {
  var lastKey: Option[(Int, String)] = None
  seq.groupBy(s => {
    lastKey = lastKey.map((p: (Int, String)) =>
      if (p._2.equalsIgnoreCase(s)) p else (p._1 + 1, s)) orElse Some((0, s))
    lastKey.get
  }).toSeq.sortBy(q => q._1).flatMap(q => q._2)
}

(使用equalsIgnoreCase作为比较函数的示例)