Instead of arguing the pros and cons of OOP vs FP and how each of the previously mentioned languages handle being OOP and FP, I’d like to introduce a very powerful Scala idiom: pattern matching. Note that pattern matching isn’t something Scala invented nor that it only exists in Scala. Pattern matching can be achieved many different ways. However, the majority of the popular languages don’t put this concept at the center of their language. A few languages way before Scala rested heavily on pattern matching such as Erlang, Haskell but that’s a different story. How does Scala offers Pattern Matching, what is it and finally why is it valuable?
Scala pattern matching by examples
As its name indicates, pattern matching is used to detect patterns. Here is an example that covers a few interesting cases:
1 2 3 4 5 6 7
If you’ve never seen any Scala that probably looks like gibberish to you. Let me break it down:
I define a new function called
listAnalysis which takes an argument
list which is of type
List (this list could contain any kind
The implementation of this function is a pattern match on the list
The body of this ‘pattern match’ looks like a classical switch statement.
But it’s actually much more than a simple switch statement. Surely it
could be used like one, but as we will see, it can do much more.
Note that you can apply a pattern match against more than one object at once as shown a bit later.
Let’s look at the statements inside the function.
In this case we are checking that the list is empty or nil. If that’s the case, the statement on the other side of the “fat arrow” is executed. In this case, we return a string but we could have called another function or so whatever.
Now the second statement is much more complex and much more powerful:
Remember that we are doing pattern matching against our list object.
What we are doing here is use the
:: operator (aka cons operator) to
extract the head and the rest of the list and then we match the head
This statement could have been written different ways:
In that case we named the
tail of the list
rest, but really we don’t
care how it’s called or its value, so the sensitive thing to do is to
rewrite that statement like that:
This basically says we are looking for a list that starts by
'a' (and we
don’t care about the rest).
In this case we type match the first element of the list and check that we have an integer. Note that using the cons operator in the match cases doesn’t seem to affect performance. It would seem that at compilation, the statement are rewritten to avoid creating uneeded objects (List also implements structural sharing of the tail list). I’m not a Scala expert so someone with more experience might be able to confirm/clarify.
Now let’s look at a variant of this statement:
This is the same statement as above, but we are adding an extra condition after the match. This is quite useful when simple matching doesn’t cut it.
Finally we have a fallback statement:
For more information about the cons operator, read about the extractor objects and what they can do.
Here is the result of calling our function with different lists:
1 2 3 4 5 6
Here is another example using 2 items for the match:
1 2 3 4 5 6 7 8 9 10 11 12 13
Why is pattern matching valuable?
In short, pattern matching allows the developer to deconstruct a structure to find specific elements, in other words the pattern, needed to then constuct an object/structure or trigger a function.
It’s the opposite process of calling a method on an object. Here we start from a structure (instead of the instance of an object), this structure is just a basic struct and based on a found pattern, we then trigger a function (with access to the data if we need it). When you have a stable and known data structure, it’s often very interesting to use the pattern matching approach because you can easily expand the operations you can execute. However, if your operations are stable but the data changes, then the Object Oriented approach seems more adequate.
Besides that, pattern matching will often make your code clearer than using if/else statements. Especially in a language like Scala where you can define pattern matching function within a function and you can also pass pattern matching functions around. Like eveything else, it needs to be used with caution so the intend of the code is still understandable. That said it’s a great tool to have handy and I’ve had a lot of fun rewriting my newbie Scala code using a more idiomatic approach based on pattern matching.
I hope you enjoyed this quick introduction. You can read more about pattern matching in Scala in the following articles: (note: Ikai’s post on how he uses regexps with pattern matching is a fun read.)