Advertisement
Guest User

unapplySeq example

a guest
Mar 21st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.70 KB | None | 0 0
  1. object HelloWorld {
  2.     object GivenNames {
  3.       def unapplySeq(name: String): Option[Seq[String]] = {
  4.         val names = name.trim.split(" ")
  5.         if (names.forall(_.isEmpty)) None else Some(names)
  6.       }
  7. }
  8.     def greetWithName(name: String) = name match {
  9.       case GivenNames(firstName,secondName, _*) => "Good morning, " + firstName+" "+ secondName + "!"
  10.       case GivenNames(firstName, _*) => "Good morning, " + firstName + "!"
  11.       case _ => "Welcome! Please make sure to fill in your Full name!"
  12.     }
  13.  
  14.    def main(args: Array[String]) {
  15.       val name1="Ali"
  16.       val name2="James Bond"
  17.       println(greetWithFullName(name1))
  18.       println(greetWithFullName(name2))
  19.    }
  20. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement