Category talk:JavaScript

From Rosetta Code
Revision as of 01:28, 1 January 2011 by rosettacode>Dkf (More detail)

Javascript is a functional language? --Michael Mol 10:45, 29 May 2010 (UTC)

It seems it is (too), according to several sources. --ShinTakezou 17:42, 29 May 2010 (UTC)

Effects System:

Functional programming languages try to reduce side effects to a minimum, this usually involves immutable data structures and pure functions and then dropping down to something like Monads when side effects are needed.

What about JavaScript? What does JavaScript do to reduce or limit side effects in your program?

  • No way of enforcing functional purity
  • No way of creating immutable objects
  • You can't statically type variables
  • You can't even put things into namespaces

JavaScript has the weakest control over its side effects of any language I know of, there is even implied globals if you leave off the var keyword.

First Class Functions

JavaScript has first class functions. But so does Lua, Perl, Ruby, Python as of 1994, PHP 5.3, Visual Basic 9, C# 3.0, and even C++0x. If JavaScript's support for first class functions is all it takes to make it functional you should also add Visual Basic and all of those other languages which have that too. A much more interesting category would be all of those languages which don't have first class functions, C, Java, and a few other languages don't have them.

What about parameter handling features? In C++ you can declare a parameter to be const to enforce const-correctness. In JavaScript you can't modify your parameters at all, they always come in as mutable variables. There is also no parameter types or default parameters.

Additionally, most functional programming languages have implicit return so that you don't have to write out a return statement. Although the Mozilla-specific extension JavaScript 1.8 does have expression closures with implicit return.

How is JavaScript used?

JavaScript puts everything into a single mutable global namespace. There is no way to export functions into your module, or other features which would make functional programming a sane choice. As such, most of the time programmers use prototypal programming and object systems like Joose and JS.Class for code re-use. As such JavaScript is mostly a prototypal programming language, or an OOP language, and not a functional programming language.

As for Scheme, I do not think it is comparable to JavaScript. It has homoiconicity, hygienic macros, and a decent effects system (side-effect causing functions are suffixed with !). These things together make it vastly different from JavaScript. --Jhuni 0:15, 1 January 2011 (UCT)

Static typing is nothing to do with functional programming. It's an independent feature axis. You can't say that namespaces are a feature of functional programming; they're just naming features (as is your point on Scheme's ! prefix). Moreover, a great many languages that are functional actually support mutable state; it cannot be a feature that allows a decision to be made on whether a language is functional. The only thing that you might possibly have a point about is “functional purity” but you're beginning to sound to me like you're making a No True Scotsman argument, which is a logical fallacy. What I can agree though is that community practice with JS is not to program in a functional manner. –Donal Fellows 01:17, 1 January 2011 (UTC)
To me, the key marks of a functional programming language are that it allows functions (or references to them) as values, that it allows recursion, and that it doesn't require the use of side-effects to produce the results of a function. That is admittedly a loose definition that permits lots of languages to claim that they support it, but so what? There's also a class of strict functional programming languages that are far more restrictive (e.g., by being side-effect free) but they're much less useful; even Haskell doesn't make it to that level of purity (due to the IO Monad, a requirement for participating in an outside world that has state). –Donal Fellows 01:28, 1 January 2011 (UTC)