Tag Archives: Gosu

The Friday Fragment

It’s Friday, and time again for the Friday Fragment: our weekly programming-related puzzle.

This Week’s Fragment

If you coded last week’s fragment under Linux, you could check your results by running cal month year.  The unix cal utility formats calendars based on the current date or a given date.  So let’s continue our new year’s programming along those lines:

Write code to print out a month’s calendar given a month and year.

To play along, post the code for the solution as a comment or send it via email.  You can built atop last week’s solution to determine the starting day of week.  There are likely many implementations already available (including C code for the unix utility), but this is a good opportunity to try new languages or techniques.

Last Week’s Fragment – Solution

With our round of game programming, our code solutions had grown a bit larger than mere fragments.  So last week’s puzzle returned to something short and sweet, as Friday Fragments marked Christmas Eve and New Year’s Eve:

Write code to calculate the day of week (Sunday through Saturday) for a given date.  Remember to account for leap years.

This is something of a classic math puzzle, and it’s not too tough to do the calculation mentally.  Many languages have a library function for this, but calling that would be cheating.  Perhaps the simplest approach is to implement Zeller’s Congruence in the language of your choice, overcoming numeric conversions.  I coded it in newly-explored Gosu:

   uses java.lang.Math
   var month=12;   var day=31;    var year=2011;  // Your date here
   var dayNames = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }

   // Shift the year to start in March, split the year components:
   if (month<3) { month += 10; year--; }
   else         { month -= 2; }
   var shortYear = year % 100
   var century = (year / 100)

   // Zeller's: W = (k + floor(2.6m - 0.2) - 2C + Y + floor(Y/4) + floor(C/4)) mod 7
   var dayOfWeek = ((day +
                      Math.floor((2.6 * month) - 0.2) - (2 * century) +
                      shortYear + Math.floor(shortYear/4) + Math.floor(century/4)) % 7) as int
   print(dayNames[dayOfWeek])

 

Have a happy 1-1-11!

Gosu

It’s Wednesday, so there must be another new scripting language for the JVM.

Well, the latest entry in this crowded field is Gosu, a language that feels a bit like Groovy, but with Scala-like static typing.   It attempts to plug some of the perceived holes in current JVM scripting languages (static typing) and Java itself (type inference, closures, properties, delegates, etc.).  Gosu compiles to Java classes and enables good code completion and strong compile-time verification.

If you have Java 1.5 or higher installed (who doesn’t?), it’s very quick and easy to give Gosu a try.  Just download and unzip, and the gosu command line and editor environment are ready to go.   There’s an Eclipse plug-in, but since it doesn’t yet support the newer Helios (3.6) version I have installed, I skipped it for now.

If you’re like me, your eyes may roll when someone actually promotes static typing.  But Gosu’s type inferencing goes a long way to simplify code and make it feel a bit like dynamic typing.  Likewise, many other features (properties, short hand syntax, cleanups, etc.) are mainly directed toward simplifying code and reducing bloat, alleviating Java’s “endless forms in triplicate” feeling.

Just to get a feel, I coded a few small things in it, including a solution to this week’s Friday Fragment.  For just about everything I tried, it was quick, clean, and intuitive, although error messages were sometimes confusing.

Gosu’s inventors (Guidewire Software) and others have already made good use of the language in production systems, so it’s not some fly-by-night research project or half-baked alpha that’ll likely go away or get neglected.  But it’s not yet clear how much adoption it will see.  I really like the language, but I’ll probably take a “go-su? we’ll see” approach before using it further.