Keep only the lines that meet a condition.

filter-lines After splitting text into lines, `filter` keeps only the lines that pass a test, such as a minimum length.

Filtering Lines

minLen
FilterLines.scala
Replay: real traced execution (multi-file project)
object Main {
  def main(args: Array[String]): Unit = {
    val minLen = 4
    val text = "go\nscala\nrust\nhaskell"
    val lines = text.split("\n").toList
    val kept = lines.filter(line => line.length >= minLen)

    println("count=" + kept.length)
    println("kept=" + kept.mkString(","))
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val minLen = 2
    val text = "go\nscala\nrust\nhaskell"
    val lines = text.split("\n").toList
    val kept = lines.filter(line => line.length >= minLen)

    println("count=" + kept.length)
    println("kept=" + kept.mkString(","))
  }
}
object Main {
  def main(args: Array[String]): Unit = {
    val minLen = 5
    val text = "go\nscala\nrust\nhaskell"
    val lines = text.split("\n").toList
    val kept = lines.filter(line => line.length >= minLen)

    println("count=" + kept.length)
    println("kept=" + kept.mkString(","))
  }
}
  1. minLen ← 4, text ← go scala rust haskell, lines ← List(go, scala, rust, haskell)

    1object Main {2  def main(args: Array[String]): Unit = {3    val minLen→ 4 = 4 //@minLen=2, 54    val text→ go
    scala
    rust
    haskell = "go\nscala\nrust\nhaskell"5    val lines→ List(go, scala, rust, haskell) = textgo
    scala
    rust
    haskell.split("\n").toList6    val kept→ List(scala, rust, haskell) = linesList(go, scala, rust, haskell).filter(line => line.length >= minLen)78    println("count=" + kept.length3)9    println("kept=" + keptList(scala, rust, haskell).mkString(","))10  }11}
    outputcount=3
    kept=scala,rust,haskell
  1. minLen ← 2, text ← go scala rust haskell, lines ← List(go, scala, rust, haskell)

    1object Main {2  def main(args: Array[String]): Unit = {3    val minLen→ 2 = 24    val text→ go
    scala
    rust
    haskell = "go\nscala\nrust\nhaskell"5    val lines→ List(go, scala, rust, haskell) = textgo
    scala
    rust
    haskell.split("\n").toList6    val kept→ List(go, scala, rust, haskell) = linesList(go, scala, rust, haskell).filter(line => line.length >= minLen)78    println("count=" + kept.length4)9    println("kept=" + keptList(go, scala, rust, haskell).mkString(","))10  }11}
    outputcount=4
    kept=go,scala,rust,haskell
  1. minLen ← 5, text ← go scala rust haskell, lines ← List(go, scala, rust, haskell)

    1object Main {2  def main(args: Array[String]): Unit = {3    val minLen→ 5 = 54    val text→ go
    scala
    rust
    haskell = "go\nscala\nrust\nhaskell"5    val lines→ List(go, scala, rust, haskell) = textgo
    scala
    rust
    haskell.split("\n").toList6    val kept→ List(scala, haskell) = linesList(go, scala, rust, haskell).filter(line => line.length >= minLen)78    println("count=" + kept.length2)9    println("kept=" + keptList(scala, haskell).mkString(","))10  }11}
    outputcount=2
    kept=scala,haskell