Saturday, June 14, 2008

A sort of functional Javascript

I realize my blog is supposed to be about infinitely interesting Haskell programming, but this short post ties in with my previous one. I decided to convert the last Haskell program from my previous posting to Javascript. The one that processed the PCRE documentation. I was able to translate it fairly closely in spirit to the Haskell one, (minus the pure functions and pattern matching, of course), with a few useful tools. The script is wrapped up with the famously awesome jQuery Javascript toolkit, so the Javascript loads from a web page, reads the PCRE.txt from the same server, does the processing in the browser, and inserts the result as an element in the current browser page. I also added Oliver Steel's Functional Javascript library. From this, the script makes use of map, foldl, and curry.

It's entirely possible to do a sort of Functional programming in Javascript. It has closures and lambda functions. Functions are indeed first class. However, Javascript wasn't necessarily designed for this, so function calls will be relatively expensive, making an already slow running process even slower.

Better dynamically typed scripting languages might have been used for scripting the web browsers, but we're stuck with Javascript here for the foreseeable future and with a little help, it's not too bad of a situation. Light and speedy Javascript toolkits like jQuery really ramp up the programming leverage available for web programming.

Compare the Haskell version with the Javascript version.