5 ms·
Isn't JS functional?
by jjzieve 10y ago
Isn't JS functional?
- sotojuan 10y agoJavaScript has functional features (first class functions for one are done well) but I think that's about it. Side effects, mutability, etc all are allowed. Compare with Haskell, Elm, or Clojure where those things are either not allowed at all or discouraged. The recent trend of "functional JavaScript" is mostly using third party tools that emulate feature from other languages (like Immtutable.js or Ramda), but IMO the core language cannot be called a proper functional language.
- theGimp 10y agoIt is a proper functional language, just not a "pure" functional language.
- platz 10y agoDefine proper functional language, as opposed to an improper one
- sotojuan 10y agoTo be fair, everyone has their own definition of FP. For me, it just feels weird to call JS functional when it only has one "feature" of most functional languages. And at least in my college studies, the discouragement of side effects was stressed as the main feature of them.
- theGimp 10y agoTo me, as long as functions are first class members, and higher-order functions are possible, I consider a language to be functional. Everything else is a convenience and not a requirement for the functional paradigm. Mind you, I do much prefer a Lisp or Scala to Javascript, but I face no trouble writing functional code in Javascript.
- platz 10y agoOk, just be aware that by that definition, just about every language that wasn't developed in the stone ages would qualify.
- theGimp 10y agoI did not know COBOL was functional too :) Out of curiousity, what other criteria would you apply?
- sotojuan 10y agoNot the one you replied but I was taught through university and books that functional programming's most important feature is the discouragement of side effects. Even on Wikipedia it's said to be a style of programming that "avoids changing-state and mutable data". I'd put discouragement of side effects and immutable data up there with higher-order functions (HOFs). You're more than welcome to have your own definition, but it's my opinion that it's too broad since HOFs exist in some form in languages like Java and Ruby and I wouldn't call them functional though like another commented said, languages are mixing paradigms nowadays (with the exception of Haskell!).
- bunderbunder 10y agoYup. The same is roughly true for being object-oriented. The big paradigm divisions were largely a thing of the 1990s; since then languages have become increasingly multiparadigm, so that it's almost hard to find a language that isn't nowadays. I think Haskell and Golang might be just about the last remaining standard-bearers of the league of one-trick ponies.
- chadaustin 10y agoThe term 'functional language' is not terribly useful these days. You can program in a functional style in many languages, just as you can program in an imperative style in Haskell or Scheme. Last year, I wrote an article [1] talking a little bit about this, arguing that the notion of 'language paradigms' is mostly a crutch to avoid thinking critically about what a language is. [1] https://chadaustin.me/2015/09/haskell-is-not-a-purely-functional-language/ https://chadaustin.me/2015/09/haskell-is-not-a-purely-functi...
- catnaroek 10y agoA functional language is a procedural language that meets the following three additional conditions: (0) It encourages using values over objects with a notion of physical identity. (1) It encourages using procedures that compute functions: mappings from values to values. (2) It discourages distinguishing between procedures that compute the same function. Let's see how well JavaScript fares: (0) Are `[1,2,3]` and `[1,2,3]` equal or different? (1) Given `function(x) { return { first: x, second: y }; }`, are `f("hello")` and `f("hello")` equal or different? (2) Are `function(x,y) { return x + y; }` and `function(x,y) { return x + y; }` equal or different? This should tell you whether JavaScript is a functional language.
- minitech 10y ago[citation needed], because this sounds wrong: > A functional language is a procedural language this doesn’t sound authoritative: > that meets the following three additional conditions this doesn’t sound like the right distinction: > It encourages using values over objects and this doesn’t sound meaningful: > It discourages distinguishing between procedures that compute the same function.
- catnaroek 10y ago> this doesn’t sound authoritative A functional language still uses procedures, even Haskell! To see what I mean, consider the possibility of divergence (infinite loop, `undefined`, `error "foo"`, you name it). It doesn't make sense for a mathematical function to diverge. The difference between, on the one hand, Haskell, ML and Racket, and, on the other hand, JavaScript, Python and Ruby, is that the former group encourages you program with procedures that compute mathematical functions, whereas the latter group does not. > this doesn’t sound like the right distinction It doesn't suffice, but it's a precondition. For good or for bad, mathematical functions are mappings from values to values, so you can't talk about computing functions in a language without a rock-solid notion of (possibly compound) value. > and this doesn’t sound like the right example for (2) [deleted from your original post, but I'm not deleting it from here] Yes, it's the right example. Haskell and ML prevent you from accidentally distinguishing two extensionally equal functions by not letting you compare procedures in the first place. > and this doesn’t sound meaningful These functions ought to be extensionally equal in any mathematically civilized language: function foo(x,y) { return x + y; } function bar(x,y) { return x + y; } But JavaScript lets me distinguish them.
- hkjgkjy 10y agoIt is. Sadly the standard library isn't, but there are great libs. Programming Javascript with lodash/fp or Ramda is great fun, and in some places I prefer it over Clojure.
- yyhhsj0521 10y agoI think immutability is more important than "functional styles" and "function as first class citizen" to a "functional" language
- int_19h 10y agoSo, would you call e.g. XQuery 1.0 a functional language? It didn't have first-class functions at all, but it was completely immutable. Or, say, SQL sans stored procedures? I think the more appropriate term in this case is "declarative". "Functional" really implies something having to do with functions.