Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Text
Posted by: Pahay Ragh
Added: Oct 27, 2020 6:35 PM
Views: 15
Tags: no tags
  1. object Person {
  2.   def apply(fullName: String) = fullName
  3.  
  4.   def unapply(fullName: String): Option[String] = {
  5.     if (!fullName.isEmpty)
  6.       Some(fullName.replaceAll("(?<=\\w)(\\w+)", "."))
  7.     else
  8.       None
  9.   }
  10. }
  11.  
  12.  
  13.  
  14. def extractors(person: Any): String = {
  15.   person match {
  16.     case Person(initials) => s"My initials are $initials"
  17.     case _ => "Could not extract initials"
  18.   }
  19. }
  20.  
  21. val rupesh = Person("Rupesh T Raghavan")
  22. println(extractors(rupesh)) //My initials are R. T R.