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).

Cheers.

Monday, November 22, 2010

First Post

Preface:
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.