Skip to content

Instantly share code, notes, and snippets.

@rupeshtr78
Created October 27, 2020 18:35
Show Gist options
  • Save rupeshtr78/d6e1c8e3e54b278bcdf0e6228ebf4e29 to your computer and use it in GitHub Desktop.
Save rupeshtr78/d6e1c8e3e54b278bcdf0e6228ebf4e29 to your computer and use it in GitHub Desktop.
object Person {
def apply(fullName: String) = fullName
def unapply(fullName: String): Option[String] = {
if (!fullName.isEmpty)
Some(fullName.replaceAll("(?<=\\w)(\\w+)", "."))
else
None
}
}
def extractors(person: Any): String = {
person match {
case Person(initials) => s"My initials are $initials"
case _ => "Could not extract initials"
}
}
val rupesh = Person("Rupesh T Raghavan")
println(extractors(rupesh)) //My initials are R. T R.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment