2009/12/24 Carsten Führmann <c.fuhrmann@bath.ac.uk>:
Dear Mike,
Thanks, everyone for your replies! Many of you suggested the same approach as Steve, functional programming and monads. At Google, however, we use Java, C++ and Python (collectively "JCP") for programs that run on our servers and JavaScript for programs that run in our webpages. So there's not a lot of call for learning a functional programming language either.
It might be worth noting that JavaScript is a functional language. (It has a lambda operator ("function"), closures, and can pass functions as parameters and return values.) However, because it has eager evaluation, the whole monad business does not apply, at least not in the way it applies to Haskell.
In fact, JavaScript is probably the most widely used functional language on the planet.
I think you're confusing the existence of first-class functions with functional programming. Functional programming avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. It's certainly possible to write functional programs in any of these languages, but it takes a lot of conscious effort--in fact, I'd say it's harder to write a functional program in JavaScript because of the myriad of strange ways state changes occur. I'm not sure what you mean by "the whole monad business does not apply". There are lots of monads, each doing something different. There are several monadic parsers I know of in JavaScript, for instance. Here's a monad for making JavaScript be lazily evaluated instead of eager: function e(x) { return function() { return x; } } function m(x, y) { return function () { return x()(y()); } }
But there are two strange phenomena:
- Functional-programming experts keep on overlooking JavaScript (probably because it is so ugly from a theorists point of view)
Probably because it's not functional.
- Most professional JavaScript programmers fail to see the enormous functional potential of JavaScript.
It is a very strange situation: the whole world uses a functional language and almost nobody is aware of it.
Anyway, even though I am very category-prone, I must admit that category theory might be a very tough sell for the JavaScript crowd :)
Yes--most JavaScript development is done by amateurs who cut and paste someone else's code and try to tweak it to do what they want. They are not mathematicians.
Best, Carsten
-- Mike Stay - metaweta@gmail.com http://math.ucr.edu/~mike http://reperiendi.wordpress.com [For admin and other information see: http://www.mta.ca/~cat-dist/ ] Status: RO