Here is my original codes with Scala.