views:

251

answers:

9

Just because functions are first class objects, there are closures, and higher order functions, does Javascript deserve to be called a Functional Programming language? The main thing I think it lacks is Pure Functions, and it doesn't 'feel' like other functional languages, like lisp (although thats not really a good reason for it not to be a functional langauge...)

A: 

Well, I wouldn't say it's functional programming, but then I would say it's object oriented and just today a friend said he wouldn't put it on that shelf either.

So, while I wouldn't say it is, I guess there's room for opinion. It does have classical features of functional programming, it doesn't have others.

Santiago Lezica
JavaScript is object oriented. OO does not require classes, it does however require objects.
+6  A: 

I would say that it is a multi-paradigm language.

EDIT: It's multi-paradigm and includes functional constructs.

Niki Yoshiuchi
yeah, I agree it's a mix and several different things.
townsean
but that doesn't answer the question of whether it is **also** functional. Being multi-paradigm implies supporting multiple paradigms. Is one of these paradigms functional programming?
jalf
yes, maybe its ok to say 'it has some features, but not all'...
hvgotcodes
+12  A: 

Repeating my own answer to a similar question,

There's no accepted definition of functional programming language.

If you define functional language as the language that supports first class functions and lambdas, then yes, JavaScript *is* a functional language.

If you also consider the factors like support for immutability, algebraic data types, pattern matching, partial application etc then no, JavaScript *is not* a functional language.


I'd encourage you to read the following related blog posts (and also the comments below them):

missingfaktor
+1 for pointing out that there is no universal definition and bringing some examples of archetypical functional language features JS doesn't have.
delnan
Later versions of Mozilla's implementation of JavaScript (staring with 1.7) have pattern matching in the form of destructuring assignments: https://developer.mozilla.org/en/New_in_JavaScript_1.7#section_20
echo-flow
A: 

To me, Javascript is both an imperative language and a functional language, and you can choose to use it either way, and even (egad) both ways. Or you can choose to use one paradigm and never touch the other. It's up to you. I, like you, don't think Javascript should be called a Functional Language, because it allows you to wander in and out of the functional programming paradigm. Perhaps if it had a pragma of some kind, to limit you using only functional programming paradigms, then that would be useful, I think. But, in summary, I say it's more of a imperative/procedural language with some functional programming features tossed in.

codeboy2k
By that reasoning, F# can no long be called functional.
Eric Mickelsen
Right. According to Wikipedia, F# is exactly what I just called Javascript: "F# [...] is a multi-paradigm programming language [...] that encompasses functional programming as well as imperative object-oriented programming disciplines"
codeboy2k
A: 

Javascript is to a point. It truly depends on how you go about programming it. If I code in an OO manner, would it not be OO? So if you just code things in a 'functional' manner it would be functional. I guess it is multi-paradigm language so to call it just one thing isn't entirely accurate.

Glenn Nelson
A: 

if you stretch and twist the term "functional programming" to the point of philosophical discussions, this question may be open again. However, then you end up on the level of useful questions like "Is C++ really a programming language"?

The answer to your question on more daily level is "no".

Functional programming means that the program is conceptualized as a evaluation of a function, rather than a control flow. The code is a description of functions, and has no inherent concept of a control flow.

JavaScript has got a control flow and is conceptualized as a imperative language. From its design objective, it is clearly not a functional language.

design objective? What do you mean? Last I checked, one of its sources of inspiration was Scheme. I'd say it's pretty clear that one of its design objectives was to support functional programming *as well* as a bucketful of other paradigms
jalf
It supports functional programming as much as C++ does, if you write appropriate foundations for this by yourself - as much as you can emulate imperative syntax in, say, Haskell with a little work. Nevertheless the syntax of JavaScript leads to it being thought of a a work flow rather than the evaluation of a function. For that reason, I (or most functional programmers) regard applying the term "functional" as too extensive.
@user411768: so you're saying that whether or not a language is functional depends on the design of its standard library? I've never heard *that* definition before. Java has most of the tools needed to program in a functional style (closures and anonymous functions, for example), which C++ (currently) doesn't. I think that makes JS a lot more FP than C++ is. The fact that the language doesn't *force* you to program in a FP style doesn't make it "less functional", does it?
jalf
(i) The standard library is a part of the standard, just as the syntactical features are, and emphasizes a certain idiomatic and conceptual style. E.g. "C++ with STL" is very different from "C with classes". It has an impact. (ii) JavaScript features object orientation, first-class-citizen functions - the features are rather orthogonal to the imperativ/functional-dichotomy. However, neither does it directly implement currying, nor does it provide purity, nor has it ever been intended for this. (iii) My last words on that, see first paragraph of the post.
+1  A: 

I tend not to think of programming languages as having one particular paradigm, but that they lend themselves to certain paradigms. However just because they lend themselves to a particular paradigm doesn't mean you have to use that paradigm. It's quite possible to write object oriented programs in C and write imperative programs in ML. Not using a certain paradigm to solve a problem because the language isn't designed for it is just artificially limiting yourself (of course you should still take into account the limitations of a language when deciding if a particular solution will be a good solution).

Nali4Freedom
+1  A: 

The term "functional programming" language is so overloaded these days it's almost useless. There are two dominant meanings:

  1. Has first-class functions
    • Javascript is this!
  2. Is based on functions as used in the lambda calculus, with an emphasis on avoiding persistent mutable state (often replacing it with parameters passed to functions)
    • As commonly written, Javascript is not remotely this!

Pick your meaning and then the question is answerable.

Chuck
Is there a source which uses "functional programming" to refer to languages with functions being first-order citizens?.
@user411768: Actually, another answerer linked to Wikipedia's article, which uses that definition. http://en.wikipedia.org/wiki/Javascript — Joel Spolsky also implied this definition in his "Can your programming language do this?" post on the benefits of "functional programming"
Chuck
A: 

What I really hate in javascript (if You try to look at it as FP language) is this:

function getTenFunctionsBad() {
  var result = [];
  for (var i = 0; i < 10; ++i) {
    result.push(function () {
      return i;
    });
  }
  return result;
}

function getTenFunctions() {
  var result = [];
  for (var i = 0; i < 10; ++i) {
    result.push((function (i) {
      return function () {
        return i;
      }
    })(i));
  }
  return result;
}

var functionsBad = getTenFunctionsBad();
var functions = getTenFunctions()
for (var i = 0; i < 10; ++i) {
  // using rhino print
  print(functionsBad[i]() + ', ' + functions[i]());
}

// Output:
//   10, 0
//   10, 1
//   10, 2
//   10, 3
//   10, 4
//   10, 5
//   10, 6
//   10, 7
//   10, 8
//   10, 9

You need to understand JS stack environment (I don't if it is the right term) to understand such a behavior.

In scheme for example You just can't produce such thing (Ok, ok -- with the help of underlying languages' references You can make it):

(define (make-ten-functions)
  (define (iter i)
    (cond ((> i 9) '())
          (else (cons (lambda () i) (iter (+ i 1))))))
  (iter 0))

(for-each (lambda (f)
            (display (f))
            (newline)) (make-ten-functions))
petraszd