Functional Programming Languages

Ever done functional programming?  Chances are you'll say "no", but you'll probably be wrong.  Javascript is a functional language, and while a lot of people use it in a procedural and/or object oriented way (\me raises hand), it's foundation is functional.  Same deal with ActionScript.  Used Groovy?  Ruby?  Python?  None are functional (let alone pure), but all have significant functional aspects.

So what is a purely functional language?  In a nutshell, it's a language that doesn't have the concept of mutability.  Things never change in a functional environment, all that happens is that new things are derived from old things.  Here's an example, which should be familiar to anyone who has done Groovy/Ruby/Python or used jQuery/Prototype:

nameArray = userObjectArray.pluckField("firstName").sort()

No mutation.  Only derivation.  It's like magic.  What's happening is the pluckField method is creating a new array with first names, and then the sort method is creating a new array in sorted order and that third array is what is set to the 'nameArray' variable.

This is ridiculously powerful, because it eliminates scoping issues from the mix.  There is no place a race condition can crop up anywhere, because everything is immutable.  What does that mean to you the developer?  It means the end of worrying about thread safety.  No more locks or scoping issues (e.g. 'var' in CFML).  It also results in really readable code.  Start at the left, read every word, and if you have decent function names it's pretty obvious what happens.

Of course, sometimes change is good, so most functional languages have the concept of mutability.  The pure ones are typically reserved for the math nerds, since algorithms in a pure functional language can actually be "proven" just like a mathematical proof.

I don't really know what my point is, except that CFML continually pisses me of with it's lack of any type of functional nature.  It sort of has higher order functions, but since it lacks closures, they're of minimal utility.  Even currying would give them some basic helpfulness, though certainly a kludge.

I was writing a simple Google Charts wrapper (to replace an SVG/Batik engine) on an app that doesn't have Groovy available to it and it's such a friggin' pain.  It's almost enough to make me want to go build a CFGroovy Lite that I can cheat into place more easily than the full framework.  Not that Groovy is even close to purely functional, but it's easy to use as if it is.

If only Clojure wasn't Lisp; I don't have that many parentheses.  A JVM-based functional language with a C/Java-style syntax would be truly excellent.

4 responses to “Functional Programming Languages”

  1. Cody Caughlan

    > A JVM-based functional language with a C/Java-style syntax would be truly excellent.

    Sounds like Scala.

  2. Peter Bell

    Guess we'll just have to wait for the GHC to be ported to the JVM :-)

  3. DeepDown

    Actually, if userObjectArray is an instance of a CF-component, the code above could be written in CF. The component methods should return "this" in order to make the chaining work :)

    Send me an email if you'ld like to have a demo CFC.