Advertisement
Guest User

Sequence Extractor

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