Wednesday, January 5, 2011

Fluent AST Transformation

I've been tinkering with my own Groovy AST Transformations of late, largely as a masochis.. academic exercise. They're incredibly powerful, but sisyphean to learn and implement. Nevertheless, by some amalgam of code and black magic, I've managed to construct a few working examples. For that, I have to thank Hamlet D'Arcy, whose efforts on the AstBuilder undoubtedly reduced the learning-curve to something approaching my pain threshold.

Most of my projects have been rather trivial; automatic method timers and the like (Nice for ProjectEuler problems, at least). However, I have recently made something (while still trivial) that may actually be of use to someone other than myself..

Although neither as idiomatic, nor as robust as it surely should/could be, I've created a simple AST transformation that automatically implements a Fluent Interface on the target class(es).  It's a very basic idea, with an equally basic implementation:  By automatically creating setters that return the instance of the property's declaring class, one can chain methods together into a more "natural flow."

For example:
import northover.*

class Person{
    def first
    def last
    int telephone

def person = new Person().setFirst("Joe").setLast("Smith").setTelephone(123456789)

The transformation also plays nice with Groovy's built-in @Delegate, which can produce some interesting results:
import northover.*

class Example{
    @Delegate String first    
    boolean modified = false

assert new Example().setFirst("  A String  ").setModified(true).trim() == "A String"

Of course, Groovy already possesses all manner of convenience when it comes to properties, so the true utility of this endeavor is arguable.  Using "with" for instance, would also cut down on the usual property noise, though by a an entirely different mechanism.

Nevertheless, I've created a repository from which the code may be obtained by anyone so inclined to toy with it.  Use it as you will.   I welcome any comments or criticism - I'm still very much a novice when it comes to AST, so any suggestions or corrections would be appreciated.

I hope someone finds it useful (or at least interesting).

Wednesday, November 24, 2010

Since every programming blog must have a quicksort implementation...

The following demonstrates a simple quicksort implementation written in Groovy.

def qsort(list){
    if ((size = list.size()) < 2) return list
    def groups = list.groupBy{it <=> list[size>>1]}.withDefault{[]}
    qsort(groups[-1]) + groups[0] + qsort(groups[1])

While not particularly optimized (Though not terrible), it is quite concise.  The use of 'groupBy' allows one to whittle down the typical iterate-and-check strategy rather nicely, though I had to add an empty list as a default to avoid passing null (which occurs if the pivot is an edge-case).

This version will sort anything that implements the Comparable interface (like the built-in sort method), as evidenced by the use of the colloquial "UFO" (<=>) operator.

This is all, of course, simply an academic exercise - More to demonstrate groupBy and withDefault than anything else ( two methods that I'm finding eminently useful in an increasingly wide variety of situations).


Monday, November 22, 2010

First Post

I'll refrain from making the usual prognostications about the future of the blog and the content therein.  In truth, I've no idea what will become of it, if anything.  Writing is not something I typically do for pleasure, thus blogging is, at best, something of a masochistic enterprise for me.  Nevertheless, I hope this blog can be of some benefit (Principally my own - But, if someone else should find use of a post, all the better).

For now, I'll begin with the usual (somewhat abbreviated) introductions...

I'm a college student, at least in theory.  I recently received an Associates Degree in Computer Science, and hope to be on my way towards a B.S. (Or better.. Maybe) shortly.  In the meantime, I keep myself busy reading and writing "hobby" code.  A good proportion of which consists of Project Euler problems; something that will likely become a fixture of this blog. Or not. See: "Preface."

My language of choice is typically Groovy, but I also enjoy Haskell, and more recently, Scala. All of which are great languages, at least in my admittedly naive opinion.  Though, I've found all three have rather disillusioned me on Java, which in it's turn, soured me on C++ - There's some grain of truth in the adage "Ignorance is Bliss."

I've also dabbled in a number of other languages, including: Ruby (Namely JRuby), VB.NET (Not one of my favorites), and C ( Working on a hulking MUD game about 12 years ago). I'll occasionally slap together some HTML/CSS/JavaScript (JQuery) - Though you wouldn't know it to look at this blog layout, at the moment.

...All of this said, I remain a woefully inexperienced programmer, and anticipate remaining so while I continue with college.  There's simply no substitute for the real world, it seems.