Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object HelloWorld {
- object PreferredNames {
- def unapplySeq(name: String): Option[Seq[String]] = {
- val names = name.trim.split(" ")
- val greetNames=
- if (names.size < 2) names
- else names.last+:names.head+:names.drop(1).dropRight(1)
- if (names.forall(_.isEmpty)) None else Some(greetNames)
- }
- }
- def greetWithName(name: String) = name match {
- case GivenNames(firstName,secondName, _*) => "Good morning, " + firstName+" "+ secondName + "!"
- case GivenNames(firstName, _*) => "Good morning, " + firstName + "!"
- case _ => "Welcome! Please make sure to fill in your Full name!"
- }
- def main(args: Array[String]) {
- val name1="Ali"
- val name2="James Bond"
- println(greetWithFullName(name1))
- println(greetWithFullName(name2))
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement