admin管理员组文章数量:1131236
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...)
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...)
Share Improve this question edited Aug 7, 2012 at 12:12 hippietrail 16.9k21 gold badges109 silver badges173 bronze badges asked Oct 18, 2010 at 19:27 hvgotcodeshvgotcodes 120k33 gold badges207 silver badges237 bronze badges 10- 12 @slashmais: No! That only prevents it from being a pure(ly) functional language. ML (at least modern dialects) are impure, too - but noone would dare to call them not functional ;) – user395760 Commented Oct 18, 2010 at 19:47
- 4 There are plenty of languages that are commonly considered functional, but which aren't pure. I don't see how that's a requirement. If you want to be that strict, then most so-called OOP languages aren't OOP either. You end up with about 95% of all languages being no-paradigm languages. – Stack Overflow is garbage Commented Oct 18, 2010 at 19:58
- 7 Why does it matter though? When I code in C++, I don't care if the language "is OOP" or not. I care that it has certain OOP features, and that it has a couple of functional programming features, and a lot of imperative programming features, and a lot of generic programming features. But whether it "is-a" OOP language or a FP language or something else doesn't matter. Likewise when I code in JS, it doesn't matter if it is FP or not. What matters is that it supports a lot of nice FP features. It seems this is the wrong question to ask. – Stack Overflow is garbage Commented Oct 18, 2010 at 20:06
- 3 @hvgotcodes: so? There's absolutely no rule saying it isn't. My rule of thumb is that it is a functional language if you can use it to program in a functional style. Since Javascript has first-class functions, closures and lambdas, I believe you can, and so as far as I'm concerned, it's a functional language. Not a pure one, obviously, but neither are most of the languages we typically consider FP (SML for example). So really, I think you just need to loosen up. If that makes your eye twitch, you need to see a doctor. – Stack Overflow is garbage Commented Oct 19, 2010 at 17:11
- 3 @jalf, absolutely. The motivation for the question was i wanted to know what my peers and people who are smarter than me think. – hvgotcodes Commented Oct 19, 2010 at 17:18
15 Answers
Reset to default 190Repeating 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):
Scala is not a functional language
Erlang is not functional
Reddit discussion on "Erlang is not functional"
I would say that it is a multi-paradigm language.
EDIT: It's multi-paradigm and includes functional constructs.
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.
The term "functional programming" language is so overloaded these days it's almost useless. There are two dominant meanings:
- Has first-class functions
- Javascript is this!
- 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.
I don't think there a concrete definition of functional programming , however many of things people consider "functional programming" can be done with javascript. Here is a good brief example in this article.
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.
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).
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.
@petraszd I rewrite your code a little to obtain a "new" for operator:
function ffor(a, b, f){
function it(i){
if(i > b)return
f(i)
it(i+1)
}
it(a)
}
print("----" + new Date()+"----")
var funcs = []
ffor(0, 9, function(i){
funcs.push(function(){return i})
})
ffor(0, 9, function(i){
print(funcs[i]())
})
But I know that this way has disadvantages for big loops...
Related question about tail recurtion optimization in JS
P.S. Posted here cuz have problem with code formatting while posting as comment
As we know the functional programming language doesn't allow to change or mutate the elements(state)of functions but in javascript it is allowed in that sense it is not a functional programming language, although it does treat function as first class citizens.
First, we have to define functional programming. I define it as any language that natively supports and privileges the style of programming shared by canonical (or at least widely agreed-upon) functional languages like Scheme, Racket, Haskell or Clojure.
Other languages, like OCaml, Elixir and Scala have much deeper functional support than JS, but still tend to be considered multi-paradigm. Functionalness is a spectrum.
All of this is very much open to endless debate and nitpicking, but this definition seems solid enough to make the case that JS isn't a serious functional language and probably never will be one any more than any other modern, multi-paradigm language with first-class functions.
Let's pick a specific feature. The language should perform tail call optimization so you can natively write recursive functions on linear data structures. Almost all major implementations of JS fail to offer this fudamental feature that we'd expect from a typical "functional" language (by the above definition) and have no plans to at the time of writing (see Are functions in JavaScript tail-call optimized? for details).
Let's give the benefit of the doubt and toss in TCO and still, JS fails to offer even a slight hint of the immutability design goal you'd expect of a "true" functional language. Just getting const
in the language took decades, and all objects are mutable by default.
These problems can't really be completely resolved due to backward compatibility; it's not really possible to turn an established multi-paradigm language into a truly functional language after the fact.
JS is about as functional as, say, Python, Perl, PHP or Ruby which all offer map
/filter
/reduce
operations on lists and support first-class functions or procedures. The existence of first-class functions offers enough of an opening to write code that is in a functional programming style. Toss in trampolines and ramda.js and it might seem convincing at a glance.
The question is whether first-class functions are sufficient to make the language "functional". In fact, Wikipedia lists all of the aforementioned as functional languages, but then, that list includes just about every popular, modern, general-purpose language other than C and Go (including at least one that explicitly identifies as not functional by design) so I don't see that this definition offers much distinguishing value.
There is an interesting paper, well developped (pure functions, partial application, currying ...) and illustrated with many short examples, here: https://www.freecodecamp.org/news/functional-programming-in-javascript/
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.
In Javascript, you can do something like this!!
// Data
var fruits = [
{ name: 'apple', price: 5 },
{ name: 'orange', price: 10 },
{ name: 'lemon', price: 15 }
]
// Request Data from magicURL
request('magicURL')
.then(selectKeyOf('price'))
.then(priceMethod('sum'))
.then((result)=>{
console.log(result) // 30
})
I have made a github page in order to demo this concept and you can clone/view my implementation
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))
本文标签: Is Javascript a Functional Programming LanguageStack Overflow
版权声明:本文标题:Is Javascript a Functional Programming Language? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736760713a1951530.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论