underscoreio/essential-scala

Code mistake (C.7.2)

Closed this issue · 0 comments

C.7.2 Solution to: Titlecase extractor

The solution will not compile:

object Titlecase {
  def unapply(str: String) = {
    str.split(" ").toList.map {
      case "" => ""
      case word => word.substring(0, 1).toUpperCase + word.substring(1)
    }.mkString(" ")
  }
}

because unapply method should return value of Option[String] type:

object Titlecase {
  def unapply(str: String) = Some(
    str.split(" ").toList.map {
      case "" => ""
      case word => word.substring(0, 1).toUpperCase + word.substring(1)
    }.mkString(" ")
  )
}