add functional style solution in Scala