Tag Archives: caelum

DSL in Scala for Date Calculation

I’ve been attending a course about Java Architecture in Caelum these last weeks. In one of those Saturdays, the instructor mentioned a little bit about DSLs. Better yet, he gave us an example using Scala =)

Now, the example was intriguing and interesting, at least for a Scala beginner like me, so I decided to translate it to English (the original was in Portuguese) and post it here. If you want, you can see the original in Portuguese, created by Sergio Lopes, here.

Before seeing the translated version of the DSL implementation, lets take a look on how you would use it – which is the most interesting part:

Tomorrow minus 1 month and plus 10 years and plus 1 day

Although the code above looks like a (almost?) proper sentence, it is valid Scala source code. That’s the beauty of writing DSLs in Scala =)

One thing that took me a while to understand regarding this code is the Conjunction part. Represented by the and instance in this case, its purpose is simple (at least after you understand it): pass a partial result to the next part of the calculation, when necessary. Notice in the code how months, years and days have one overloading that receives a Conjunction. This is what makes possible to yield a result that will be passed to the next calculation step.

Here is the full implementation code of the DSL:

import java.util.Calendar

class Date(val data: Calendar) {
 data.clear(Calendar.HOUR)
 import Date.Conjunction

 private var last = 0;

 def plus(num: Int) = { last = num; this }
 def minus(num: Int) = { last = -num; this }

 def +(num: Int) = plus(num)
 def -(num: Int) = minus(num)

 def months = { data.add(Calendar.MONTH, last); this }
 def months(and: Conjunction): Date = months
 def month = months
 def month(and: Conjunction): Date = months

 def years = { data.add(Calendar.YEAR, last); this }
 def years(and: Conjunction): Date = years
 def year = years
 def year(and: Conjunction): Date = years

 def days = { data.add(Calendar.DAY_OF_MONTH, last); this }
 def days(and: Conjunction): Date = days
 def day = days
 def day(and: Conjunction): Date = days

 override def toString = "%1$Td/%1$Tm/%1$TY" format data
}

object Date {
 class Conjunction
 val and = new Conjunction

 def Today = new Date(Calendar.getInstance)
 def Tomorrow = Today + 1 day
 def Yesterday = Today - 1 day

 def today = Today
 def tomorrow = Tomorrow
 def yesterday = Yesterday
}

The only thing I added to the code was operator overloading, so that the usage can be even more interesting, allowing stuff like this:

Today + 2 months

So, the def + and the def - are not present in the original code. I just added those as an exercise to understand how to use operator overload in Scala, which ended up being ridiculously simple. If you want to learn more about operator overloading, Joey Gibson has a nice blog entry about this here.


Falando em Java 2009

Hi! This post is one more about Java related events!

Two weeks ago, we had the OpenTDC 2009. And this last Sunday, we had the Falando em Java (Speaking Java) 2009. Those two were very busy weeks!

As OpenTDC, Falando em Java was a very nice event. Interesting presentations, and a lot of nice people. A lot of them were, actually, present at both conferences.

Again, we had a handful of people from the office present:

The Team at Falando em Java

The Team at Falando em Java

The event started with some guys (sorry, I forgot their names!) talking about the history of the event, and of Caelum (the event organizer), and about how they both have been growing these last few years. Nice but could feature less marketing.

Opening

Opening

Next, we had Jim Webber talking about SOA. Very good! This was the first time I saw him talking, and he really knows what he is doing! This, and the latter one, were the best presentantions of the event, no question.

Jim Webber

Jim Webber

During OpenTDC 2009, Bruno Sousa introduced us to Juggy, and talked a little bit about the next big java event in Brazil: Just Java (and I’ll be there!). This time though, I managed to get a better picture:

Bruno & Juggy

Bruno & Juggy

Ok, not that much better, but anyway…

A couple of other presentations followed and then it was lunch time! Nice:

Lunch

Lunch

After lunch, two more presentations, a break, and then the second international one. This one was supposed to be given by Bill Burke, but he couldn’t attend due to Visa problems. So Jim Webber did it, and very nicely so. The topic: Restful Web Services.

From here on, I don’t have more pictures, my battery run out! But there were only a few things left: some gifts and the closing, plus some beer to end the day.


Follow

Get every new post delivered to your Inbox.