Grails 1.0 RC1 Has Been Released Tuesday, October 16, 2007

Grails 1.0 RC1 has been released! See the release notes for details about many of the new features.

This is fantastic news for the Groovy and Grails community.

In a few hours I am getting on a plane to London to speak at The Grails eXchange. I am really looking forward to finally meeting in person some of the folks I have been working with for so long now.

Maverick On Rails Sunday, September 09, 2007

Mark Cuban says...

"I'm such an exciting guy, I downloaded Ruby on Rails and read the documentation as well. That's what Saturday Nights are for."

Maybe Mark will come out to the upcoming NFJS show in Dallas and learn about Grails. :)

Interesting Groovy Loops Detail Tuesday, August 07, 2007

While fixing a bug in Grails last night I came across something that I found interesting. At first I was really puzzled but now that I know what is going on, it all makes perfect sense.

A unit test that had been passing for some time just started failing. I isolated a specific revision of a specific file that seemed to be causing the test to fail. The test passes with version X of the file and the test fails with version X+1 of that file. I spent some time diffing the 2 revisions and couldn't find anything that seemed relevant. Eventually I figured out the specific change that was causing the failure. There was code that looked something like this...


someCollection.each { someElement ->
// do something with someElement
}


That code had been changed to look something like this...


for(someElement in someCollection) {
// do something with someElement
}


The code inside the loop was exactly the same for both versions. The only difference is the original code was using "each" to iterate over the collection and the new code was using a "for" loop to do the same thing. I puzzled over that for a bit trying to figure out what was going wrong and after discussing with my co-developers the problem eventually became clear. Code inside the loop was making use of someElement inside of a closure and later that closure was being executed. Consider this...


def listOfClosures = []
def numbers = [1, 2, 3]
numbers.each { number ->
listOfClosures[number] = { println "number is $number" }
}
listOfClosures[1]()
listOfClosures[2]()
listOfClosures[3]()


As you might guess, the output from that looks like this...


number is 1
number is 2
number is 3


Now consider this...


def listOfClosures = []
def numbers = [1, 2, 3]
for (number in numbers) {
listOfClosures[number] = { println "number is $number" }
}
listOfClosures[1]()
listOfClosures[2]()
listOfClosures[3]()


The output from that looks like this...


number is 3
number is 3
number is 3


With the "for" loop approach, the same "number" variable is used each time through the loop so by the time the loop completes, the value of "number" is 3 which is what is used by the time the closures execute. With the "each" approach, a separate variable is used for each iteration so each closure accesses the "number" that was around when the closure was declared. None of that is broken or a bug. I believe it is all working as it should.

Thanks to Charles Nutter and Graeme Rocher for helping with that.

My Apologies To The New York City SIG Friday, July 20, 2007

This past Wednesday night Andy Glover and I were scheduled to co-present at The New York Java Special Interest Group. Andy was going to give a 90 minute session on Groovy and I was going to give a 90 minute session on Grails.

Wednesday turned out to be crazy day in New York. Weather made it difficult for air travelers to make their way in to JFK Airport. The huge steam pipe explosion that happened in mid-town Manhattan made it very difficult to drive in to Manhattan. The whole story is a very long one but the short of it is that Andy's flight was canceled and so was mine. We couldn't get a second flight booked for Andy but we were able to get one booked for me. Unfortunately mine was an indirect flight and the second leg was delayed numerous times and I finally got to JFK to find that because of the steam pipe explosion in the city, the trip from JFK to the meeting location was well over an hour and by the time I got into the city, there was no point. I think the meeting was scheduled to run until about 8:00 and I think I got into Manhattan shortly after 8:00.

I was really disappointed myself as I have never had the opportunity to work with Andy before this event and I was really looking forward to it but mostly I felt bad for all of the developers in New York who made it to the show only to find out that there were no speakers. The New York developer community is really enthusiastic about Groovy and Grails and the large audience that turned up to see the show represents that. I am sorry that they were left to look at an empty stage Wednesday night. I promise all of them that we were scrambling all day to work it out and each time we came up with a solution another problem came up, mostly related to shut downs at JFK. I hope that many of the folks who came out to the SIG meeting will make it to the NFJS show in Princeton New Jersey the weekend of August 10th. I will not be there myself but my friends Jason Rudolph, Scott Davis and Venkat Subramanium will all be there with Groovy and Grails sessions.

Again, I offer my apologies to everyone who came out to the show Wednesday evening and I hope that you will invite us again.

Alternate Syntax For URL Mapping In Grails Saturday, May 19, 2007

One of the cool new features that was introduced with Grails 0.5 is Custom URL Mapping. The syntax for declaring the mapping looks something like this...


class MyUrlMappings {
static mappings = {
"/product/$id" {
controller = "product"
action = "show"
}

"/$blog/$year/$month/$id" {
controller = "blog"
action = "show"
constraints {
year(matches:/\d{4}/)
month(matches:/\d{2}/)
}
}
}
}

Since the release we have added support for an alternate syntax (which I happen to like) that looks like this...

class MyUrlMappings {
static mappings = {
"/product/$id" (controller:"product", action:"show")

"/$blog/$year/$month/$id" (controller:"blog", action:"show"){
constraints {
year(matches:/\d{4}/)
month(matches:/\d{2}/)
}
}
}
}

Dynamic languages like Groovy make these sorts of things really easy to support. Behind the scenes of this url mapping there there is no grammar file and no tangly parser to manage. The configuration file is plain old groovy code. Blurring the line between code and configuration files by using a DSL like this is so much easier in Groovy that it might be in a language like C++ or Java. You have got to love it. :)

Autism Visibility Continues To Increase

Dover Speedway has announced that the NEXTEL Cup race coming up there in June will be named “The Autism Speaks 400 presented by Visa”. NEXTEL Cup racing is huge here in North America and has a very large fan base. Autism Speaks is one of the largest foundations in the world that is dedicated to autism. I am happy to see them get their name on the marquee of an event like this. That can only help with awareness.

This is not the first time that autism and automobile racing have crossed paths. Jamie McMurray has an autistic niece and partnered with The Autism Society of American in 2004 when he drove the “Drive For A Cure” car. Tony Renna drove the “Cure Autism Now” car in 2003. There have been others.

It has been almost exactly 5 years since my youngest son Jake was diagnosed autistic. In those 5 years I have learned a number of things about autism and one of those things that I have learned is that there are not a lot of people who know much about autism. Awareness is increasing. That isn't because more people are reading medical journals in their free time. Aside from a dramatic increase in the number of kids being diagnosed autistic, the biggest reason that awareness is increasing is that more and more autism is showing up in the mainstream media. Recently some actors and other celebrities have been criticized for jumping on the “autism bandwagon”. These criticisms often refer to autism as the disease du jour. I don't have much to say about that except to say that raising awareness is an important part of this fight and when I see people going out of their way to help with awareness, I am happy to see that.

Cool New Features In Grails 0.5 Tuesday, May 01, 2007

Grails 0.5 was released today and this release is one of the most exciting releases yet. This release has closed the gap on a number of key features that needed to be knocked out before the coming 1.0 release. Some of the new features will be more appealing to some projects and other features more appealing to other pojects. For me some of the coolest new features added in 0.5 are Command Objects, List and Map Support in GORM and Custom URL Mappings. Hundres of JIRA issues have been addressed in this release and significant performance enhancements have been made. The development team got a whole lot done during this iteration. See the release notes for a more complete list of new features.

With the recent release of the first beta of Groovy 1.1, things continue to heat up in the Groovy and Grails communities.

This year I have been speaking at Java User Groups, internal training events and on the No Fluff Just Stuff Tour about Groovy and Grails and I am finding that folks in the Java community are really excited about both Groovy and Grails and the possibilities that these technologies bring to their projects. After a bit of time that really seemed kind of dull for the JVM, Groovy and Grails are making it all fun again and at the same time adding a lot of value to the enterprise by increasing productivity and expanding the possibilities. For example, defining a DSL using Antlr or similar technologies may not have been a realistic possibility for many applications but dynamic technologies like Groovy make things like that not only possible but simple to build. There are many many examples just like that which represent ways Groovy can help developers get their jobs done faster and more simply. Frankly, I don't think Grails would be anywhere near as compelling as it is if it weren't for all of the coolness made possible by Groovy. Grails has taken the possibilities that Groovy provides and leveraged them all over the framework to provide simple concise techniques for web application developers to use to build powerful web applications very quickly.

Grails has earned a lot of respect in the community by getting very far along in a short amount of time. While Grails applications are already being deployed in production environments, both private and public facing sites, the coming 1.0 release is going to push Grails right out there to the front of the pack. This is exciting stuff!

More to come... :)

I Have Been Tagged (Twice) Friday, April 06, 2007

I have been tagged twice recently. First by Jason and now by James. I suppose I need to be a good citizen and play along. :)

I am not really sure what the rules are here. I know that I am supposed to list several things that you probably don't know about me. I don't know if these things are supposed to be funny, embarrassing, personal, impressive, inspiring or what. I don't know if knowing the rules would help me figure out what to write anyway so I am just going to jot out some facts and hope that I don't suffer some sort of penalty for any violation I might commit. :)

I am a member of The American Go Association. I love playing Go, although I am not able to make as much time for it as I might like. My commitment to Go seems to come and go with periods when I will play a lot and periods when I don't play much at all. My friend Lianzhou Yu moved out of state a couple of years ago and that has affected my playing time. Lianzhou is one of the top players in The United States and was a great mentor to have access to. A few years ago my friend Brian Gilstrap and I got together in his wood shop (which I am envious of) to build a couple of Go boards. I am pretty happy with the way my board turned out. I wish I played on it more often. As it is, most games I play these days are played on my laptop against GNU Go.

I won the first large poker tournament I ever played in. The tournament was a charity event to benefit The National Alliance For Autism Research (now Autism Speaks). Having an autistic son, I was happy to contribute and participate in the tournament. There were about 130 entrants. When I heard about this event I gathered up the group of guys that I played cards with regularly and we ended up with 11 of us in the tournament. The final table at the tournament (final 10 players) included 4 guys from our group. In one of the final hands I made a mistake and then ended up getting pretty lucky to beat my friend Paul Jensen out of a big pot and that piece of luck put me in a position to win the tournament.

My name is Jeff, and I am a metal head. There, I said it. I am a fan of heavy metal music. Black Sabbath, Iron Maiden, Slayer, Sepultura, Opeth, Lamb Of God... all guilty pleasures.

I enjoy fly fishing. I tie all of my own flies. I fish mostly for trout in the cold water streams and rivers in Missouri. My late father-in-law introduced me to fly fishing more than 10 years ago. I find less time for fly fishing these days but I really enjoy myself when I do make time to get out in the water.

There was a time long ago when I really, really, really needed a haircut. Why didn't anyone approach me and say something like “friends don't let friends have mullets”?

Now I am going to throw to Dean, Lance, Brian, Eric and Nate.

My Mac OS X Bling Saturday, February 17, 2007

I am now about 2.5 weeks in to my MacBook Pro owning experience. I will say that I am really really happy with the experience so far. I have tinkered with the idea since OS X was introduced and finally decided to give it a go. I am going to provide a rundown of apps I have installed and use. This is everything (I think) that I use that isn't part of the standard OS X install that came with the hardware. If you are an OS X user then at least some of these you already know about but maybe there will be a couple new ones here for you.

Firefox
To be honest, I didn't even give Safari a shot. One of the first things I did was download Firefox. Maybe I should look at Safari, but I probably won't unless at some point I hear/read something that gives me some specific reason to do so.

Desktop Manager
This is a virtual desktop switcher with nice effects (bling) when switching desktops like the rotating cube and others. VirtueDesktops has some nice features but it crashed more than once for me. I may look again when the next release comes about.

TextMate
TextMate is The Full Schizzle. If you edit text (xml, java, c, ruby, groovy, screenplays, etc...) TextMate is for you. If you aren't familiar with TM, visit the site and watch some of the screencasts.

SnapZ Pro X
This is really nice software for recording screencasts.

Adium
Adium is an IM client that allows me to connect to MSN, Yahoo, AIM, Google Talk, ICQ, Jabber and others.

Skype
Skype is another messaging client. The only reason I have this in addition to Adium is that I have a meeting once a week with some folks overseas and we do that over Skype using its group voice capabilities.

NeoOffice
OpenOffice for the Mac. I haven't used it enough to say for sure if I really like it, but so far it is fine.

iRed Lite
iRed Lite is a utility that lets me use the standard MacBook remote control to control more applications than it otherwise does, like NeoOffice, PowerPoint and others.

QuickSilver
A slick utility similar to the built in Spotlight but a little nicer to use, at least for some things.

Spanning Sync
This lets me sync my desktop iCal with my public Google calendar, which I really like. The main site says "coming soon" but betas are available now.

Goban
I don't play computer games, except for Go. This is a nice Go client.

Blender
An awesome 3D animation tool. The movie Elephant's Dream was created with Blender. The entire movie is distributed under a Creative Commons license so you can get all of the production files for free and monkey with the movie if you want to.

StuffIt Expander
I don't have much to say about this one but I use it and include it here for completeness.

Standard dev tools that really aren't peculiar to OS X but I happen to use on my MacBook Pro...

Eclipse, NetBeans and IntelliJ IDEA
I have installed all 3 IDEs. I use Eclipse primarily. I installed NetBeans because I am about to start doing NetBeans Platform development and need to get familiar with the environment. I installed IntelliJ for no particular reason other than I have a license for open source development, though I haven't used it in a couple of years.

I also use Groovy, Grails, JRuby, Ant and the Subversion Client.

I would be happy to hear about your favorite OS X applications.

Groovy And Others Continue To Expand The JVM Horizon Saturday, January 20, 2007

We have had the JVM for over a decade now. There are a whole lot of languages out there that allow developers to build applications for the JVM. The most popular of course is Java. The alternative languages cover the whole spectrum including Lisp, Scheme, Logo, Tcl, Cobol, Ada, Python, Forth, Fortran, Pascal and on and on and on. Why would so many people spend so much effort developing new languages for the JVM or bindings for existing languages to run on the JVM when Sun already went to the trouble of developing the Java language? That is a fair question. The answer varies from case to case. Many of the languages built for the JVM were academic exercises but certainly not all of them. Part of the answer is the fact that the JVM is a fantastic platform for deploying applications but the Java language is not always the best solution for the job at hand.

Among the wide array of languages for the JVM, a few languages have poked their heads up as key players lately. In particular, dynamic languages are getting a lot of attention, for good reason. Of those dynamic languages at this point Groovy and JRuby are among the most popular.

JRuby is a pure Java implementation of the Ruby programming language bringing the power and flexibility of Ruby to the Java platform. One of the significant contributors to Ruby's success is the Ruby On Rails web application framework. JRuby promises to bring that power and flexibility to the JVM. JRuby is a great tool for folks who want to write Ruby code for the JVM but JRuby is not what I want to talk about today. I want to talk about Groovy...

Groovy is a dynamic language written specifically for the JVM. Groovy has a syntax that in many areas is going to be really familiar to Java programmers. Like Ruby has Ruby On Rails, Groovy also has a framework for agile web development and that framework is Grails. Grails has taken a whole lot of inspiration from Ruby on Rails and is a really powerful and fun way to build web applications for the JVM. Grails is bringing the "coding by convention" paradigm to Groovy web programming in a way that is really appealing to developers already familiar with Java's syntax and the rich capabilities of the JVM.

So What Is Happening With Groovy And Grails?

So much is going on with Groovy and Grails right now. The Groovy community has been on track for a long time now knowing that 2007 was going to be a big year. Now that 2007 is here, Groovy and Grails are really in their groove. ;)

Here are some of the things stirring in that community right now:

- Groovy has just released version 1.0. The Groovy community has known that this was coming and having 1.0 out there on the shelves now is a big win for Groovy.

- Manning has recently published Groovy In Action, known as GINA. GINA is being referred to as "Groovy's Pick Axe Book" (a reference to Dave Thomas' definitive guide to Ruby, Programming Ruby). That is not because GINA was the first major book published on Groovy. This has more to do with GINA's clear, direct and thorough coverage of the language.

- Apress has recently published The Definitive Guide To Grails. In The Definitive Guide, Graeme Rocher presents Grails with a real nuts-and-bolts feel that takes developers through comprehensive coverage of the Grails framework.

- Morgan Kaufmann has recently published Groovy Programmers: An Introduction for Java Developers. I have not had time to read Kaufmann's book yet but I think having another general coverage book out there is probably a good thing.

- Jay Zimmerman and Scott Davis have gone live with http://www.aboutgrooy.com/, a great one stop shopping portal for all things Groovy and Grails.

- InfoQ recently published Jason Rudolph's minibook Getting Started with Grails, providing another source of Grails coverage and bringing Grails to the attention of more folks.

- The 2007 No Fluff Just Stuff Symposium Tour is dedicating significant track time to both Groovy and Grails. NFJS has always responded to the community by presenting what developers know is going to be important and the timing is just right this year for the tour to bring Groovy and Grails to the forefront.

- Skills Matter has announced the first 3 day Grails eXchange 2007 event. Grails eXchange focuses on Groovy and Grails in addition to providing tracks dedicated to Java Entperise Edition (JEE) and Ajax/Web 2.0.

- Sven Haiges has announced that his series of Grails podcasts is going to expand to include general Groovy coverage. Sven's podcasts have been a great asset to the Grails community and opening that up to include more Groovy coverage is going to be great for the community.

- Some recent podcasts of interest include Scott Davis' interview with Guillaume Laforge, an interview with Jay Zimmerman and an interview I did with Sven Haiges for the Grails Podcast series.

Developers and Enterprises that are interested in building applications for the JVM are going to find that Groovy solves a lot of their problems really well. All of those Java developers out there will find that learning "The Groovy Way" is not a difficult task and the benefits are fantastic. Combine all of that with the fact that writing Groovy code is just plain fun and you have got a recipe for success.

If you don't already know why so many people are getting excited about Groovy, pickup a copy of GINA and start tinkering. As you start developing your Groovy Kung Fu you will start to question many aspects of "the old way" that have been taken for granted for so long now. Many things about Groovy "just make sense".

Have Fun!

Next Grails Release Coming Soon Wednesday, January 10, 2007

After a bit of a development slowdown around the holidays the Grails development team is back at full throttle working on the next release. Version 0.4 is probably going to be ready to ship within the next few weeks. Version 0.3 was a really significant release in terms of new functionality and addressing some key issues that had been lingering. I feel like 0.4 is even more significant. The team is working hard to add new functionality and also filling in a lot of those little nuisance gaps.

Things are really ramping up in the Grails community. We are seeing folks coming up with interesting plugins. The mailing lists are very active. If you haven't taken a look yet, give Grails a spin. The Quick Start Guide doesn't even scratch the surface of capabilities but is a good first step. Following that guide will take about 10 minutes and you will have a simple CRUD application up and running.

Stay tuned for exciting things from Grails in the coming months.

Groovy 1.0 Has Been Released! Tuesday, January 02, 2007

Finally, after a lot of waiting and a lot of work from the Groovy dev team, Groovy 1.0 has been released.

Thanks to the Groovy dev team for working through all of the challenges, hassles, debates, hard work etc. that were all part of getting to 1.0.

2007 is going to be a big year for both Groovy and Grails. I am proud to be a member of the Grails development team and I am looking forward to what 2007 holds in store for us.

As one of my mentors is fond of saying... Press On!

Groovy 1.0 RC2 Released Saturday, December 23, 2006

Groovy 1.0 RC2 was released today. If all goes well over the next few days the 1.0 release should be available by year's end. The community has been waiting a long time for this. All of the hard work and patience from the Groovy development team is about to pay off. Thanks Guys!

Have A Groovy Christmas!

The Definitive Guide To Grails Saturday, November 25, 2006

The Definitive Guide To Grails is finally available from Apress. The book is a well written easy read that provides a great foundation for Grails developers.

Manning's Groovy In Action (GINA) has been available through their Early Access Program for several months now. The final version of GINA will be available in the next month or so.

Things are heating up in the Groovy and Grails communities. Look for a lot of exciting things to be happening around Grails soon.

Grails 0.3 Released Thursday, November 09, 2006

Grails 0.3 was released today. A lot of work went in to this release which addresses over 100 bugs and feature requests. In my opinion, the fact that Grails is currently at 0.3 is a little misleading. The framework is very powerful and stable. There is a lot on the horizon but the tool is very powerful in its current state. Give it a spin.

I have recently joined the Grails development team and I am looking forward to what the future has in store for Grails.

Grails Support For HTTP Method Restrictions Saturday, November 04, 2006

I have committed code to Grails to support a declarative syntax for imposing restrictions on which controller actions may be invoked using which http request methods. See the docs for details.

Grails Enhancement Submitted Saturday, October 21, 2006

I have worked up an enhancement to Grails recently that provides an easy declarative way for application authors to limit access to certain controller actions based on the http request method (PUT, POST, GET, etc...). Generally speaking, applications should not allow destructive operations to be initiated in response to a GET. That isn't the only reason to want to impose restrictions, but it is a common one. With Grails, the only way to deal with this is to put code in your controller to inspect the request object and figure out if the request was a GET, POST or whatever. For the common case where all I want to do is prevent certain actions from being invoked via a GET, I don't want to have to do that. I just want to tell the framework not to allow it. The patch I have worked up does just that. The patch allows code like this in your controller...


class EmployeeController {

// action1 may be invoked via a POST
// action2 has no restrictions
// action3 may be invoked via a POST or DELETE
def httpMethodRestrictions = [action1:'POST',
action3:['POST', 'DELETE']]

def = action1 { ... }

def = action2 { ... }

def = action3 { ... }

}


The patch has been attached to http://jira.codehaus.org/browse/GRAILS-379.

Grails Presentation Thursday Evening Saturday, September 16, 2006

My Grails presentation Thursday evening went very well. Weiqi Gao blogged about the presentation in real time.

The presentation slides are available at http://www.ociweb.com/javasig/knowledgebase/2006-09/. The tone of the whole session was very light and fun. You will see in the notes that I included quotes about Grails from famous people such as John Lennon, Mr. T, Batman and Robin and even Elvis Presley. Each time a celebrity came up in the slides I asked a trivia question about that celebrity and the first person to shout out the correct answer was given a copy of the Groovy In Action MEAP, courtesy of Manning Publishing. Thanks to Manning for those.

There are not many code samples in the presentation slides. During the presentation I built a simple application and along the way applied the concepts that are mentioned in the notes.

Before the presentation there was some discussion among the group about the future of Java and a lot of people in the group agreed that dynamic languages are going to be an important part of what we do in the future.

Learning New Languages, Like Haskell (not Eddie) Wednesday, September 06, 2006

There are a number of reasons for developers to learn new programming languages. One reason is to keep their skills current. Another reason is that many developers simply find learning new languages to be fun. Another reason is that learning new languages forces developers to think about problems differently. That is what I want to discuss.

Learning new spoken languages changes the way people think about problems. Learning new programming languages is no different. When a C++ developer learns Java they can't do pointer arithmetic any more. They can't use multiple inheritance in the same way. What about Ruby and Groovy? The fire marshall is going to shut down the Ruby bandwagon because it is way over capacity right now. I don't think there are any Java developers left who haven't at least tinkered with Ruby. Java developers learn Ruby and then realize things about Java that start to seem fundamentally wrong. Why isn't there a simple syntax in Java for declaring properties like you can in Ruby or Groovy? Why are there so many 1 line getters and setters in the Java world? What about that dynamic typing? That takes some getting used to. On and on... More languages... More examples...

Languages like C++, Java, Ruby and Groovy are all very different languages but at the same time, are all pretty much the same. They are all object oriented. When you write a program in one of those languages you model the business objects, encapsulate logic, pass references around and all of these objects collaborate to solve a problem. OO has been around for a pretty long time now and is an effective way to build systems. If learning different OO languages is beneficial (and it is), what about learning fundamentally different languages? That ought to be valuable as well. I say it is anyway.

I recently spent a little time playing with BF. That is interesting stuff but no sane person is every going to propose that is a good way to write anything. However, learning BF is an interesting exercise. Try to write a simple calculator in BF and you will be forced to think about things differently. Learning BF is strictly an academic exercise.

On a more practical front I have been playing with Haskell lately and am finding it very interesting. Haskell is a functional programming language. Functional programming languages are all about the function. Haskell is a real programming language that is used to build real systems, not just a goofy language to play around with (like BF). At first the language may seem prohibitively useless for its lack of things imperative programmers are used to. For example, there is no destructive assignment in a pure functional language. That means there is no such thing as "x = x + 1". What? How can I write software without basic functionality like that? You can. This "feature" isn't missing from the language (or whole class of languages), it just isn't necessary.

My Haskell Kung Fu is nowhere near sharp enough to provide any kind of tutorial but I will tempt your curiosity with some very basic hello world kind of examples. Take a look at this...


fac 1 = 1
fac x = x * fac(x - 1)


That is a way to write a function in Haskell to calculate factorials. The first line says "factorial 1 is equal to 1". The second line says "factorial of any other number is that number multiplied by the factorial of 1 less than that number". That seems pretty straightforward, doesn't it? If you read those 2 lines of code out loud, it reads almost like you would describe what a factorial is.

If you can follow the factorial example, the fibonacci example below shouldn't be difficult to understand...


fib 0 = 0
fib 1 = 1
fib x = fib(x - 1) + fib(x - 2)


I don't know how functional programmers really think about that but my OO mind thinks of that as 3 overloaded versions of the "fib" function. One takes a 0 as an argument, one takes a 1 as an argument and the other takes any other number as an argument. This could be written in haskell with just 1 function and some "if" blocks but the code above is "the functional way".

A slightly more complicated example is a sort routine like this...


my_sort [] = []
my_sort (x:xs) = my_sort less_than_x ++ [x] ++ my_sort greater_than_x
where
less_than_x = filter (<x) xs
greater_than_x = filter (>=x) xs


The first line there says that the result of sorting an empty list is an empty list. That seems reasonable to me. The rest is another "overloaded" (probably not the terminology the functional crowd would use) version of the same function. This version accepts a list as an argument where x is the first element in the list and xs is the rest of the list. That little syntax turns out to be useful a lot. The result of sorting that list is achieved by concatenating (++) 3 things. The first thing is a sorted copy of everything that is less than x. The second thing in the concatenation is x. The third thing in the concatenation is a sorted copy of everything that is greater than x (actually greater than or equal to x, as we'll see shortly). The labels "less_than_x" and "greather_than_x" are just that, labels. There is no Haskell magic at play there. The lines after the "where" clause define what those labels refer to. "less_than_x" is defined to be everything in xs that is less than x. "greater_than_x" is defined to be everything in xs that is greater than or equal to x. Since "less_than_x" and "greater_than_x" need to be sorted before the concatenation takes place, recursion is taking place.

Some may look at that code and immediately conclude that it is confusing to look at and can't possibly be a good way to write code. However, once you understand how each of those pieces work, this is really a pretty direct expression of what is being accomplished.

Maybe you will take some time to look at Haskell and find a lot of interesting things about it. If you are really feeling funky and want to explore functional programming constructs in Java, take a look at FunctionalJ. That is interesting to think about but I think from the perspective of tweaking your brain a bit, for a lot of folks learning Haskell is probably a more interesting and more valuable experience.

Enjoy!

Obscure Programming Language Sunday, August 20, 2006

I was doing some research on quines this weekend and while doing so I stumbled across several obscure programming languages that I was not previously familiar with. The following is actual code I wrote that will compile and run (it isn't a quine)...


>++++++++++[>+++++++>+++++++++++>+++++++
+++>++++++++++++>+++>++++++++[<]>-]>-.>+
++++.>-.+++++.---.>-.>++.>-.<<<---.++++.
<++.--.>---.--.<+.>++++++++.<-----.-.>>>+.


Do you know what language that is? A few hints...


  • The language is composed of just 8 commands, each expressed with a single character (7 are used in the program above)

  • Whitespace is insignificant

  • The language is "Turing-Complete"

  • The language has a colorful name (if you reply to this, you may use the initials to avoid the offensive word)