Scalaで文字列の整形

Scalaで文字列を左右または中央揃えに整形するには???


解法
■左寄せ

def ljust(s: String, n: Int): String = {
  if (s.length < n) {
    return (s + (1 to (n - s.length)).map{ _ => " " }.mkString)
  } else {
    return s
  }
}    

val spam = "Spam"
println(ljust(spam, 10))

■右寄せ

def rjust(s: String, n: Int): String = {
  if (s.length < n) {
      return ((1 to (n - s.length)).map{ _ => " " }.mkString + s)
  } else {
      return s
  }
}

val spam = "Spam"
println(rjust(spam, 10))

■中央寄せ

def center(s: String, n: Int): String = {
  if (s.length < n) {
    return { ((1 to ((n - s.length) / 2)).map{ _ => " " }.mkString) +
            s + 
             ((1 to (n - (((n - s.length) / 2) + s.length))).map{ _ => " "}.mkString)
           }
  } else {
    return s
  }
}

val spam = "Spa"
println(center(spam, 10))