RSS

How to use extractors in Scala for powerful pattern matching – Devoxx style

04 Jan

A few months ago, Joshua D. Suereth gave a very nice technical talk at Devoxx 2013, titled “How to wield Scala in the trenches“, which was full of functional programming & Scala gems. The last slides of his presentation were dedicated to a very powerful style of using extractors and pattern matching. I liked his example so much that I wanted to understand it better and note it down in my blog so that I can apply it in many similar cases.

The basic motivation comes from the following question. Assume you have a List of objects, e.g. People, who can have more than one residence; and you want to select the ones based on a condition, e.g. the ones living in Istanbul. There are of course different ways to traverse the list and filter the ones you are looking for:

Wouldn’t it be nice to do pattern matching over that List in an almost natural language-like manner, such as:

and get a result such as

List(Ergin, Ahmet)

Thanks to extractors, it is possible to achieve the intended effect by defining LivesIn and Istanbul like

You can find a self-contained example, PatternPower.scala which you can compile and run at the end of this blog entry. This technique, presented by Suereth at Devoxx 2013, seems simple and fundamental once you understand it, like many advanced programming idioms. It also shows that with proper abstractions and usage of constructs, Scala proves itself again in the field of readable code. For those who want to learn about extractors used in pattern matching in Scala, I suggest The Neophyte’s Guide to Scala Part 1: Extractors.

PatternPower.scala:

About these ads
 
7 Comments

Posted by on January 4, 2014 in Programlama

 

Tags: , ,

7 responses to “How to use extractors in Scala for powerful pattern matching – Devoxx style

  1. Arnold deVos

    January 5, 2014 at 06:01

     
    • Emre Sevinç

      January 5, 2014 at 13:58

      Hello Arnold,

      Great examples indeed! Almost Prolog-lie :-) (even better). Thanks for sharing.

       
  2. Joe Kelley

    January 10, 2014 at 01:51

    Certainly a neat feature, but could you explain more when and why it should be used? For example, this seems more clear to me:

    val peopleInIstanbul = people filter (_.residence map (_.city) contains “Istanbul”) map (_.name)

    It’s not that much more verbose where you’re doing the actual filtering, but avoids the mental overhead of objects and classes used only for pattern matching. When read “as english” I agree that the pattern matching way seems nicer, but when read “as code” I think it’s a bit less clear.

     
  3. Emre Sevinç

    January 10, 2014 at 17:21

    Hello Joe,

    The existence of a feature certainly doesn’t necessarily mean that you have to use it every possible case. In my example it was for demonstration purpose, showing the power of extractors (via unapply). The examples given by Arnold deVos are even more advanced (see his comment above), almost Prolog-like. For me the common theme is obvious: the ability to name things is powerful and when the pattern matching / extracting rules get complicated, I would like to name them so that they can be used in natural looking manner. (PS: One of the audience at Josh Suereth’s talk made the comment that he used a similar technique of extractors in pattern matching for complex log file parsing and processing.)

     
  4. SteveG

    January 11, 2014 at 02:17

     
    • Emre Sevinç

      January 11, 2014 at 13:43

      SteveG,

      Sounds like music to my ears ;-)

       
  5. Arnold deVos

    January 11, 2014 at 04:13

    Joe and Emre, sometimes a chain of filters and maps is more direct and sometimes pattern matching with extractors is more clear. When the logic has alternatives (ie or operations, disjunctions) pattern matching gains an edge. – Arnold

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 57 other followers

%d bloggers like this: