How to implement 'takeUntil' of a list?

后端 未结 6 1097
傲寒
傲寒 2021-01-05 01:52

I want to find all items before and equal the first 7:

val list = List(1,4,5,2,3,5,5,7,8,9,2,7,4)

My solution is:



        
6条回答
  •  借酒劲吻你
    2021-01-05 02:36

    Here's a way to get there with foldLeft, and a tail recursive version to short circuit long lists.

    There's also the tests I used while playing around with this.

    import scala.annotation.tailrec
    import org.scalatest.WordSpec
    import org.scalatest.Matchers
    
    object TakeUntilInclusiveSpec {
      implicit class TakeUntilInclusiveFoldLeft[T](val list: List[T]) extends AnyVal {
        def takeUntilInclusive(p: T => Boolean): List[T] =
          list.foldLeft( (false, List[T]()) )({
            case ((false, acc), x)      => (p(x), x :: acc)
            case (res @ (true, acc), _) => res
          })._2.reverse
      }
      implicit class TakeUntilInclusiveTailRec[T](val list: List[T]) extends AnyVal {
        def takeUntilInclusive(p: T => Boolean): List[T] = {
          @tailrec
          def loop(acc: List[T], subList: List[T]): List[T] = subList match {
            case Nil => acc.reverse
            case x :: xs if p(x) => (x :: acc).reverse
            case x :: xs => loop(x :: acc, xs)
          }
          loop(List[T](), list)
        }
      }
    }
    
    class TakeUntilInclusiveSpec extends WordSpec with Matchers {
      //import TakeUntilInclusiveSpec.TakeUntilInclusiveFoldLeft
      import TakeUntilInclusiveSpec.TakeUntilInclusiveTailRec
    
      val `return` = afterWord("return")
      object lists {
        val one = List(1)
        val oneToTen = List(1, 2, 3, 4, 5, 7, 8, 9, 10)
        val boat = List("boat")
        val rowYourBoat = List("row", "your", "boat")
      }
    
      "TakeUntilInclusive" when afterWord("given") {
        "an empty list" should `return` {
          "an empty list" in {
            List[Int]().takeUntilInclusive(_ == 7) shouldBe Nil
            List[String]().takeUntilInclusive(_ == "") shouldBe Nil
          }
        }
    
        "a list without the matching element" should `return` {
          "an identical list" in {
            lists.one.takeUntilInclusive(_ == 20) shouldBe lists.one
            lists.oneToTen.takeUntilInclusive(_ == 20) shouldBe lists.oneToTen
            lists.boat.takeUntilInclusive(_.startsWith("a")) shouldBe lists.boat
            lists.rowYourBoat.takeUntilInclusive(_.startsWith("a")) shouldBe lists.rowYourBoat
          }
        }
    
        "a list containing one instance of the matching element in the last index" should `return`
        {
          "an identical list" in {
            lists.one.takeUntilInclusive(_ == 1) shouldBe lists.one
            lists.oneToTen.takeUntilInclusive(_ == 10) shouldBe lists.oneToTen
            lists.boat.takeUntilInclusive(_ == "boat") shouldBe lists.boat
            lists.rowYourBoat.takeUntilInclusive(_ == "boat") shouldBe lists.rowYourBoat
          }
        }
    
        "a list containing one instance of the matching element" should `return` {
          "the elements of the original list, up to and including the match" in {
            lists.one.takeUntilInclusive(_ == 1) shouldBe List(1)
            lists.oneToTen.takeUntilInclusive(_ == 5) shouldBe List(1,2,3,4,5)
            lists.boat.takeUntilInclusive(_ == "boat") shouldBe List("boat")
            lists.rowYourBoat.takeUntilInclusive(_ == "your") shouldBe List("row", "your")
          }
        }
    
        "a list containing multiple instances of the matching element" should `return` {
          "the elements of the original list, up to and including only the first match" in {
            lists.oneToTen.takeUntilInclusive(_ % 3 == 0) shouldBe List(1,2,3)
            lists.rowYourBoat.takeUntilInclusive(_.length == 4) shouldBe List("row", "your")
          }
        }
      }
    }
    

提交回复
热议问题