Search This Blog

Scala Pattern Matcher, why should I care!?

Scala Pattern Matcher in a stupid simple definition is a switch++. So it's like a switch statement but far more powerful and fun :-) Why the hack should I care about this, well there some reasons. Instead of just writing down advantages and scenarios that you use it I will should what you can do with a Pattern Matcher in practices trough code.

Did you know we can use pattern matcher in order to do recursion, oh yeah, we can easily use to do recursion, whats the advantage, well the code gets cleaner and also easy to read and understand, check this Factorial code written using pattern matcher out.

Code is cool because the if inside the pattern matcher cover the case when factorial of 1 is one 1but also use that as a breakthrough the recursion I mean that's also the exit clause. All in the same package, very compact with great expressiveness.

Like Python Scala has tuples. You basically use tuples in order to return multiples values in a function, so If you don't want to create a class to deal with a function return you can use a tuple. Scala pattern matcher support tuples and use can use the case statements adding more elements to get the kinda tuple you want or event specify the types.

Dead easy, as you can see you can get based on the number of arguments, we can specify all types for all arguments or just few types for some few arguments.

IF you write down a function that's use a pattern matcher straight you may combine that with other function in a high order function fashion and get a great readable code really nice to look, like this.

So I created a List with String, the days of the week and as you may already know Scala List has lots of nice functions one of they is the foreach function, for each item inside the list the function whatTodo will be called with the current element of the list, this is Scala syntactical sugar. Since '.' and '(' and ')' are optional. You could to this code explicitly them will be something like this.

Remember the "_" is the quantification wild card and will be replaced by the current element of the list. I definitive prefer the very first way of do this, granted that if you do this with several functions could get messy them I would like to introduce the parenthesis back but not the '.'. Yes kinda of Clojure Feelings !!!

I'm not even thinking about arguing that you need pattern matcher to do high order functions or to get scala syntactical sugar that's was more like a side comment, if you don't use the matcher you will end up having if statements in this case I will stick with the matcher.

We can write more advanced type comparison as well like this one right here. In this case I'm using case classes that are special kick ass classes in Scala that can be part of pattern matcher :-)

So as you see, there are several possibilities, we can get a very specific String or a case class with a specific value or even use the "default" here expressed by the 'case _' statement.

We also can combine the pattern matcher with if conditions but all surrounded by the matcher scope. This just add more power to the matcher. We also can return values from the matcher, in all my sample you often see me printing things, I did that because if the simplest sample but we can return values as well.

This is one of other things that Scala was inspired by Haskell and ErLang Matcher. Functional programming is definitely a great strength of Scala :-)

The Pattern Matcher works with literal values and types and some wild card expressions but it also works with your local variables, you just need specify the scope in my case if object scope so I do that with 'this'.

Scala Pattern Matcher is powerful it's another functional programming thing, what's cool is that Scala bring that to the OO world as well. And you can use in plenty of cases. I hope you had enough fun so far, see you next time.

Cheers,
Diego Pacheco

Get link

Facebook

Twitter

Pinterest

Google+

Email

Other Apps

Popular posts from this blog

Akka a great actor framework for the JVM. Akka is high performance with high throughput and low latency, resilient by Design. It's possible to use Akka with Java however I always used and recommend you use with Scala.

Create actors systems is pretty easy however creating multiple nodes on Akka cluster locally could be boring and error-prone. So I want to show how easy is to create an Akka cluster using Scala and Docker(Engine & Docker-Compose). We will create a very simple actor system, we will just log events on the cluster however you can use this as a base to do more complex code.