Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 26: Extractorsย >ย 26.2 Extractors - Pg. 598

C HAPTER 26 ยท Extractors This works, but is kind of clumsy. What's more, things would become more complicated if you combined several such tests. For instance you might want to find two successive strings in a list that are both email addresses with the same user. You can try this yourself with the access functions defined previously to see what would be involved. You saw already in Chapter 15 that pattern matching is ideal for attacking problems like this. Let's assume for the moment that you could match a string with a pattern: EMail(user, domain) The pattern would match if the string contained an embedded at sign ( @ ). In that case it would bind variable user to the part of the string before the @ and variable domain to the part after it. Postulating a pattern like this, the previous expression could be written more clearly like this: s match { case EMail(user, domain) => println(user +" AT "+ domain) case _ => println("not an email address") }