Scala Exercises

Apache Spark 2 Workshop

@jaceklaskowski / StackOverflow / GitHub
Books: Mastering Apache Spark / Spark Structured Streaming

Scala Exercises — Hints

  1. Use scaladoc at http://www.scala-lang.org/api/current
  2. Use sbt console or Scala Worksheet (in IntelliJ IDEA)
  3. Write a test first before you code and sbt ~test (mind the tilde)

Exercise: Println Command-Line Arguments

  1. Use args to access command-line arguments
  2. Use foreach and println to println elements

Exercise: Removing Elements From Collection

Problem: Remove Every Third Element


val labels = Seq("hello", "world", "Boston", "Warsaw", "London")

def removeThirds(labels: Seq[String]): Seq[String] = ???

val result = removeThirds(labels)

// Seq("hello", "world", "warsaw")
// Hint: Use zipWithIndex and filterNot
            

Exercise: Println Lines From File

  1. Problem: Show the content of a file specified on command line
  2. Read a file using scala.io.Source
    1. fromFile
    2. getLines
  3. Print results
    • Seq.foreach
    • println

Checkpoint

## Additional Questions * What's **_._1**? * What's **map**? * What's **flatMap**? * What's **foldLeft**? * Can I and when to use **reduce** instead of **foldLeft**?

Questions?