Monday, October 25, 2010

DSL Magic

Updated: Work is being done for a cron dsl work on my project cronish. Check it out... fork me... I'm open to suggestions.

I have never taken a strong interest in creating DSL's before tinkering with Scala. I had an eye opening experience this weekend, that originated as a joke.

I've recently taken an interest in the Harry Potter series. Other Potter fans may also know that the next one in the series is coming out next month. On my birthday, in fact.

I wanted to write a program that simply let me ask my computer when the movie was coming to theaters. Scala is perfect for this. The DSL was thrown together in a hurry, so I am not particularly fond of the underlying code, but the finished product was this:

scala> import com.philipcali.potter.dsl._
import com.philipcali.potter.dsl._

scala> When is "Harry Potter and the Deathly Hallows: Part 1" coming out?
Harry Potter and the Deathly Hallows: Part 1 is coming out in 24 days!

scala> When is "Harry Potter and the Deathly Hallows: Part 1" in theaters?
Harry Potter and the Deathly Hallows: Part 1 is coming out in 24 days!

What exactly is the point? I can ask my computer when a movie is coming out casually, and it will tell me... through Scala. The DSL is rather pointless for a Scala programmer. Using this simple DSL, a non programmer could code something, and that is the point I am getting to. The usefulness behind the DSL.

That's when it hit me!

A cron DSL. Imagine being able to do something like this:

scala> val schedule = "every day at 1:00pm".cron
schedule: java.lang.String = "0 13 * * *"

scala> "every half hour on the 19th of November".cron
res0: java.lang.String = "30 * 19 11 *"

scala> "every hour on every Thursday in December, January, and February".cron
res1: java.lang.String = "0 * * 12,1,2 4"

Once the parser is created, configuring a scheduler through annotations, actors, or other third party software would be a piece of cake. I plan on using Scala's built-in support for language parsing to achieve this goal. And a new project was conceived.

-- Philip Cali

1 comment: