Monthly Archives: April 2010

Scala Pattern Matching: the reason to fall in love with Scala

Scala has a lot of interesting features and, among them, one of the most interesting ones is Pattern Matching. If you still need a reason to try Scala this is the one. Actually, I might be exaggerating a little bit, but this feature is really interesting. Really. ;)

In summary, Pattern Matching looks like switch statements in Java. But there are a couple of things to keep in mind:

  • Using switch statements in Java is usually a code smell, i.e., something to avoid, risking your good OO design. This is not the case in Scala, where pattern matching is a core feature of the language, and should be leveraged by your applications.
  • Unlike in Java, there is basically no limits in terms of what you can use in the case clause. This, combined with the fact that Scala is a functional language, letting you pass functions around, gives you insane flexibility.

Lets go through a (very) simple example so that you can see by yourself at least part of this power. Suppose you are writing a function that takes an object of any type, and prints some information about it. Here is how you could do this:

class PrettyPrinter {
  def print(obj: Any) = {
    obj match {
      case 1 => println("number one")
      case Client("john") => println("special client: john")
      case Client(name) => println("client: " + name)
      case other => println(other)
    }
  }
}
case class Client(name: String)

object PatternMatchingTest {
  def main(args: Array[String]): Unit = {
    var printer = new PrettyPrinter
    printer.print(1)
    printer.print(new Client("jcranky"))
    printer.print(new Client("john"))
    printer.print("RandomStuff")
  }
}

Notice that, in the same clause, we are matching:

  • an integer number;
  • a specific Client
  • any other Client
  • any object of any class

Very flexible. For instance, in the case where we match any client, notice also that we capture the client’s name “automagically”, and use it in the println call.

That’s all for now. What do you think? Is this powerful enough for you? =D

EDIT:

I forgot to mention the expected output of the code above… so here it is:


number one
client: jcranky
special client: john
RandomStuff


Globalcode’s Casual Class: Digital TV

I just came out from one more of the Globalcode‘s casual classes. The topic this time: Digital TV.

It usually takes me a few days before I write something about an event I attended. Well, its time to change this. But why now?

One of the speakers at the event was Dimas Oliveira. He opened the event (unofficially) asking the audience several different questions that seemed somewhat disconnected at first. But in the end he came to a conclusion that is similar to my reasoning to have this post out quickly. Timing.

Dimas Oliveira

Dimas Oliveira

Do we know Digital TV related buzzwords and/or its related technologies? Or at least know what they are? Do we want to? If so, the time is now. Actually, we might be a little late already. So, learn now, earn the dividends, or give up and move away to something else – later will be too late.

Makes sense? Hope so. Now, to some more concrete stuff. Dimas talked about opportunity, and remembered us that Ginga-J was just approved as an official Digital TV system (system? framework? API? I’m not sure yet about how to call it) down here in Brazil – as of a few weeks ago, actually. There is at least one TV device already available for sale, and there is certainly more to come. Good timing to get in this market for us developers.

Next, Thiago Vespa talked about BD-J – Java for Blue Ray devices. If you live in Mars, you might not know yet that Java runs on Blue Ray devices, like the Playstation 3. BD-J is the technology to use when developing applications that run on such devices. This part of the casual class was a little bit (just a little ;) ) offset from the rest.

Thiago Vespa

Thiago Vespa

Then Neto Marin talked about LWUIT. LWUIT is an API/Framework to develop user interfaces for mobile devices. He explained quickly how it works and followed to a demonstration, given by Thiago again. He demostrated how to develop an Xlet (a Digital TV application), using LWUIT interface. The point being: LWUIT was added as part of the Ginga-J specification, and thus can be used to develop the interface of any Digital TV application.

In time: today they decided to exchange the pizza time to the start of the event instead of the end. What was happening before was that we reached the end of the talkings being really hungry, and couldn’t pay too much attention to the last speakers. Only the beers stayed at the end this time. Good call – I can’t talk for the rest of the attendees, but I liked the change.

No promises, but I might try to play a little bit with Ginga-J. If so, I’ll post whatever results I get here. See you next time!


GridGain first impressions

I’ve been working on a peer-to-peer project for a while. We are trying to build something new and interesting regarding transactions on top of a peer-to-peer network. Hopefully it will be useful =P. I’m not going to talk about that project now; the important point is that I’m aware about how hard setting up and configuring peer-to-peer nodes can be. And making everything work out of the box seamlessly, with an easy to use API at the same time.

A couple of weeks ago, GridGain came to may attention, totally out of the blue. Now imagine my surprise when I decided to test it, following some of their examples. The freaking thing is easy to use and works out of the box. Run a script, a node is running. Do it again, a second one is running. Of course, for more complex scenarios you will have to configure a few things, but the point is that I could starting testing it right away.

The way to build and “deploy” services is ridiculously easy as well. I coded the Hello World example. When running, you start a new node in your main class, and in it you say what tasks you want to execute. GrigGain will then find all other available nodes and, depending on the task implementation, split the jobs between those nodes. Something I liked here is that the task implementation is sent to the other peers in a completely transparent way – you don’t even notice what’s happening.

I won’t dive too much into GridGain here, nor show any code, simply because of another great quality it has: documentation. There is a lot of stuff available, including a lot of easy-to-follow examples. Take a look for yourself. I won’t be using this framework for now, but if I end up needing to implement grid-related stuff, I certainly know where to start looking.

Direct link to the GridGain documentation.


Follow

Get every new post delivered to your Inbox.