Matt Follett

When Type Systems Attack

Groovy’s type system can sometimes have funny interplay and unexpected results. For example, these assertions are true:

However, mixing in a Map into this causes some complications:

This might not seem like a big issue, but it can cause some real headaches. When failing an assertion the message printed only prints values, not types. Because of this you may occasionally run into less than helpful error messages like:

And this can be awfully confusing. One prime example of this is with GORM domains. GORM defaults primary keys to be Longs so if you use a primary key as a map key you could run into some pretty confusing error cases that your tests don’t explain as helpfully as you’d like.

Grails/DatabaseMigrationPlugin/Liquibase Quirk

I just fought with this for a while and figured I’d record this in the vast internets in case others come across this. While attempting to extend the functionality of our Grails project at work I needed to add a new table and related foreign key constraints. We use the Grails Database Migration Plugin which uses liquibase to keep our schema in sync between production, staging, and development boxes. I declared a foreign key constraint similar to this one:

and got this exception:

It turns out that passing in a named parameter of referencesUniqueColumn with a value of anything other than false causes this exception. This wasn’t immediately obvious to me and I still don’t know why, but I thought I’d note it in case someone else ran into it too.

Grails Custom Validation Messages

Grails’ GORM provides convenient ways to define many different constraints for inputing properties including basic ones like max, equal, & notEqual to more specific ones like creditCard, url, & email (full list here under Quick Reference). But sooner or later you’ll want to create your own custom validator. This can be done by providing a validator closure to your domain, for example:

Now, in this example a domain will validate all of the fields, including the user’s favorite prime (to a degree of certainty). However, if the prime number certainly isn’t prime then the error message provided when validation fails (through validate or save) will be:

favoritePrime does not pass custom validation.

That probably isn’t a helpful message to eventually pass back to the user, they might guess what the mysterious “custom validation” is in this case, but certainly not in all others.

So what is the solution? You might try passing an error message back, but that won’t work; you’ll get the same message again. It turns out the proper approach is to return a key from your file. If the key exists in the proper localization file (e.g. French is then it will use that string. A simple modification to the validator above will solve this:

Adding the line: number {2} is not prime.

Will cause Grails to generate a much more useful validation failed message. As you can probably guess the {2} will be replaced with the value inputed by the user.

You can include additional values to your message by returning an array. The first element should be a message key (e.g. ‘’) and the subsequent entries will be interpolated into the message starting at index 3. So, if you returned ['', 5] you could have the message: number {2} is not prime, {3} is an example of a prime. And in this case if the user passed in 4 they would end up with the message:

The number 4 is not prime, 5 is an example of a prime.

Of course, if you aren’t using a prime number validator multiple times in your code it may seem silly to have a special key/value pair dedicated to primes. Another option is to use a key/value pair dedicated to that field. You can do this simply by using the code in the first example and addding a field to your file like:

Groovyist.favoritePrime.validator.invalid=The number {2} is not prime.

Much of this is documented in the validator docs.

Yammer, Scala, & Burgeoning Languages

I just finished reading this letter discussing why Yammer is going to slowly move from Scala to Java. While I’ve never done anything more substantial than basic toying around with scala and can’t say I know much about Yammer outside of what their website tells me about itself and the fact that Gabriel Weinberg seems to like it I do have some thoughts on general points hit on in the letter.

The letter that Coda Hale wrote seemed to be rather well thought out. The author goes into several criticisms of Scala which seem to boil down to:

  1. Complicated concepts in the language
  2. Community support
  3. Lack of abundance of developers
  4. Reality of the Scala-and-Java nature of real world development
  5. Build toolchain issues
  6. Performance issues

The first point and last two are very specific to Scala. Language designers have to decide between many difficult tradeoffs when designing a language and it is hard to say which ones will pay off for their community in the long run. It is certainly difficult to see how a given choice will pay off in the long run and while some of Scala’s features he mentions seem pretty simple, others make me go cross-eyed trying to read explanations of them. Having said that, I still am very much a novice in the language and don’t feel particularly qualified to actually weigh in on these topics.

The lack of developers and quality of community support can be bundled together and seem to have more to do with the age and uptake of the language than at the language itself. Any language will have bootstrapping issues similar to this. And when choosing a language, it is an important aspect that should, and to some extent can, be checked out before any major development undertaking. Evaluating a community around a technology is no easy task though. I’ve certainly been in situations where the community involvement around the technology used was unhelpful, misleading, or nonexistent; it is a crummy situation to be in. The best way to know the value of a community is to be a part of it for a while.

Coda points out the fact that Yammer wasn’t really deciding between Java or Scala but between Java or Scala+Java. If we ever meet I owe Coda a beer for this. I’ve had my share of arguments with people in the past on this reality and I’m glad to see something saying it so straight forward getting the circulation this is, even if it was never supposed to. Coming from a non-Java background to a Groovy position I feel acutely aware of all the times I’ve had to dig through Java code because of an impedence mismatch between a Java concept and a Groovy one, to better understand a library that doesn’t document something well and doesn’t have a functional equivalent in Groovy, or to debug or diagnose a framework bug. Having said that, I don’t believe my team has written a new line of Java for our core product since we migrated to Groovy some number of months ago.

I expect that the Java+otherLang situation is an inevitability of developing on the JVM. As long as Java is the dominant player on the platform it will be the lingua franca. The reality of these languages being so interoperable is that the cost to benefit analysis of porting a framework or library (internal or external) over to another language on the platform almost always works against porting it.

In the end, Coda seems to make a lot of good points. I can’t vouch for his Scala specific concerns, but others seem reaonable in the more general case of burgeoning JVM languages. And, while this doesn’t lower my interest or intent of learning Scala one of these days I do wonder why Yammer decided to start with Scala given the resources available to them with other languages.

My intention has been that were I to take a startup from day 1 I’d pick a language that I already had a team, or list of would be team members, with a strong understanding of and a good history with the community. Maybe they had that int he beginning or thought they did and that wasn’t mentioned, I certainly don’t have that information. And really, in the end, they have nearly sixty million dollars in VC funding, three million customers, and a rapidly growing revenue stream and I’m killing time waiting for my next robotics assignment to come down the pipe. I guess you can’t argue with results, hehe.

Edit: Also of note is Coda’s follow up, you should read it if you read the first.

Perl to Groovy Mappings (an Introduction)

Recently I took a job with a startup that is using Groovy as their main language. I find the language to be similar to several other languages and they have some comparisons on their site, however one comparison they are missing is to Perl. Since I’m learning Groovy anyway and the two are sufficiently similar that I thought I’d give it a shot.

Before I start with the articles there are a few important things to note about Groovy:

  1. Groovy autoboxes, as such it is completely legitimate to write `”I like cheese”.someMethodOnStrings()`.
  2. Groovy automatically makes a variable named `it` as the only parameter for lambda function if no parameter list is given, meaning that: `def foo = { it * 2}` is similar to `def foo = { it -> it * 2}`.
  3. Groovy’s syntax provides an alternate way to list parameters for a method call when the last parameter is a lambda function meaning that `someData.someMethod(1,2, { it + 2})` is the same as `someData.someMethod(1,2) { it + 2}`, this is useful for methods that loop over lists, like `each`.


Perl to Groovy Mapping (Iterating)

This entry will primarily go over various approaches towards iterating over structures of data. This will include basic looping as well as equivalents to Perl’s map & grep.

This is not intended to be an exhaustive guide or a best practice for either language. The attempt, as with all articles in this series, is to provide a basic set of mappings between the languages.

Iterating over collections

To loop over all the items in an array in Perl you would probably use the for loop construct:

for my $item (@array) {…}

You could do something similar in Groovy using the each method on collections:

list.each {}

To loop over all the keys in a hash (or Map) you can combine the above with the keys builtin:

for my $key (keys(%hash)) {…}

In Groovy you simply use a lambda that accepts two parameters.

myMap.each { key, _value ->  }

Looping over all the values is similar to the examples above in both, so I will skip that. Looping over each entry in the hash with both the key and value in Groovy is the same as the example above, in Perl you would have to use each, which I don’t like.

It is important to note that with Groovy’s each different things are passed in based on the number of parameters. The example above gave each a 2 parameter lambda and so the lambda got inputs of [key, value]. Had a 1 parameter lambda been passed in then that lambda would have received an Entry object that had accessors for the key and value.

Iterating over a Set of Integers

Both Perl and Groovy provide many ways to iterate over a range. One popular approach in Perl is to use the dot-dot range operator (..) and for looping construct. By using this it is simple to print all the numbers between one and ten:

for my $num (1..10) { print $num }

Groovy has a similar approach:

(1..10).each { print it }

Groovy has a couple other ways to do the same thing, for example numbers have an upto method attached to them that can be used to iterate. As such, the same thing above could be written as:

1.upto(10) { print it }

Of course, Perl also has the ability to loop via the while looping construct and Groovy could loop using a for in on top of what I’ve listed here.

If you wanted to iterate over a set of integers that didn’t increment by one in Perl you might use map and for like this:

for my $multiple_of_five ( map { $_ * 5 } 1..5) {…}

Or you might use grep and for like this:

for my $multiple_of_five ( grep { not $_ % 5 } 1..25) {…}

Or, of course, you could pick up a useful module, like Math::Sequence.

In Groovy you would simply do this:

5.step(25,5) {}

Mapping a List

Perl helpfully provides the map builtin to easily map a list of values from one to another. For example, were a developer wanting to square all the values in a list they would:

my @squared = map { $_**2 } @original_values

The same thing can be performed in Groovy using the collect() method on collections:

List squared = originalValues.collect { it.power(2) }

Grepping a List

Perl provides the grep builtin to easily filter values out of a list and provide a new list with only the remaining values. For example, if a developer was looking for all the items in a list that were odd they could:

my @odd = grep { $_ % 2 } @numbers

To do this in Groovy they would use the findAll method that exists on collections:

List odd = numbers.findAll { it % 2 }

Grepping for the First Item in a List

Perl has a number of modules which are considered ‘core’. It is reasonable to assume any core module is installed and readily available. To get the first item in a list that matches some criteria a Perl developer could use the example above for grepping a list and shift off the top, but more likely they would use List::Util’s first like this:

my $first_odd = first { $_ % 2} @numbers

A similar approach in Groovy could be done via the find method on collections:

def firstOdd = numbers.find { it % 2 }

The Unity Model

I’m reading Parallel Programming Design:  A Foundation by Chandy and Misra for a master’s class and am finding it very interesting.  The book covers the Unity Model as an approach towards parallel programming.  The language used to model solutions to a problem isn’t meant to be directly implemented as a language (though it has been incarnated as PCN) but instead to be used as a model for implementing the solution in some other language.  It is a fun language and hopefully now that I don’t have a presentation looming over my head I can explore it independently and post some example.

The language is broken up into several parts:

  1. Declare - used for declaring variables
  2. Always - used for defining functions
  3. Initially - used for initializing variables
  4. Assign - the main portion of the code

The reason why initialization doesn’t happen in the main portion of the code is that there is no guarantee that it would happen when you would want or that it would only happen once.  In fact, the assignment section of the program has very weak promises.  It only guarantees that when the program is executing any given line that any other line will eventually be executed if the program is run forever.  Other than that it just randomly picks statements to execute. 

By the way, the book has been out of print for a while now, so it isn’t the easiest to find for cheap and is impossible to find new.  I ordered it three times used from third parties before someone finally actually had it in stock and shipped it to me.

If You Like X, You Might Like Perl

Objective-C - You’d probably like Moose’s concept of roles if you like categories.  Roles also provide the ability to require methods exist on classes so you could use them for interfaces too, but when you consume a role you can’t guarantee it won’t add functionality.

Clojure - If you were willing to play with Perl 6 you’d probably like the many of its lazy aspects.  The common example being binding an infinitely long fib sequence to a list (my @fib := 0,1,*+*…* ).  Perl 6 also supports multi-method dispatching and pattern matching.  I don’t know about Clojure’s pattern matching abilities, but I do know that Perl 6’s is weaker than Erlang’s, so be aware of that.

If you don’t want to try Perl 6 you should look at the Higher Order Perl book.  It focuses on functional Perl and steals heavily from Lisp.  In fact, the author states very early on that of the 7 things that make Lisp different, Perl has 6.  It is available free online or from a book store.  The author is currently boycotting Amazon though, so while they sell it you may want to buy from someone else. 

Ruby - I haven’t gotten around to learning Ruby.  For what it’s worth, there are a lot of people I know that like both Ruby and Perl so I have to assume there are some good similarities.  If you are looking for method_missing it is called autoload.  Besides that, Moose::Exporter is great for making keywords for DSLs.  You can use Perl 5’s prototypes, but that gets a little more complicated.  You can also use Devel::Declare if you just want to take the parser over altogether which, of course, is gets more complicated.

Feel free to add more in the comments or tell me where I’m wrong, I’ll update the body of the post as the conversation goes along.

7 Languages in 7 Weeks in St. Louis

One of the guys at the Genome Center mentioned he wanted to get a 7 Languages in 7 Weeks group started in St. Louis to go through the languages.  I thought that was a neat idea, but I also realized it was very simple and may not require a group.

If you look at the list that Tate provides for Tate’s book, he lists these languages:

  1. Ruby
  2. Io
  3. Scala
  4. Erlang
  5. Clojure
  6. Haskell
  7. Prolog

If you look at SLUUG’s St. Louis Event Calendar you find that:

  1. Ruby - Meets 2nd Monday of the Month
  2. Io
  3. Scala
  4. Erlang - Is now dead due to lack if interest, but could be resurected (I know since I ran it :) )
  5. Clojure - Meets 4th Thursday of the month
  6. Haskell - Had its first social, maybe it can take off with meetings soon
  7. Prolog


Sadly, we don’t have a Scala, Io, or Prolog group that I know.  However, I bet that we could get people to volunteer for such talks in Lambda Lounge.

As an addendum, if STLRUBY didn’t want to do a session I’m sure St. Louis Perl Mongers would be willing to shift around their schedule to do a talk on dynamic typing and meta programming.


15 Minutes of Fame

Holy cow!  I decided to check my Google Analytics account to see what the impact of my If You Like X You Might Like Perl.  I thought it might, maybe, double the visitors to this page.  I was quite wrong, here is how wrong:

[[posterous-content:yhofHwAcfwbthvkqAcAn]]So I started looking around at what drove this traffic.  It turns out that perlgeek posted my article to reddit.  When I followed the referring links I found a few interesting things.  Apparently the article is the 16th most controversial at the moment.  It is below whether ads are like reality, but above some insult against redit users, some act that played out in the war in Afghanistan, and the POTUS’s policy on cannabis:

[[posterous-content:uEljCjbzqDglzvjGlHms]]It is also 24th on top scoring links in the programming category, but there aren’t any interesting juxtapositions so I’m not providing a screenshot.  Honestly, I find the whole thing so amusing that I don’t mind that the link has received more negative votes than positive ones or that 4 of the 5 comments are either negative, useless, or both.  The fifth is asking if one of the other commenters actually read the article, which is really neither positive nor negative.