Hacker News new | ask | show | jobs
by ksikka 3896 days ago
> not picking a functional language

As opposed to what? In what ways do you not consider Javascript to a be a functional language?

2 comments

Sure, Javascript has first class functions, which I guess meets the bare minimum requirements for being a functional language, but it also lacks some key features:

* Pure functions - JS functions are essentially subroutines. They might return a useful value or they might mutate a bunch of stuff and interact with the outside world. Who knows? JS functions aren't guarunteed to return the same value, or any value at all.

* Immutability - Anything and everything can be mutated in JS, including the contents of objects that are defined using the new ES6 `const` keyword

* Curried functions by default - Although it's possible to compose or partially apply JS functions, it's not nearly as nice as Haskell or Elm.

You're influenced by only one part of FP world. Let's take Lisp, or even better: Scheme.

   * pure functions - all you said is equally true for Scheme
   * immutability - set-cdr! and many SRFIs: vectors, boxes, records...
   * curried by default - nope, not in Scheme either
There are other flavours of FP languages other than ML descendants. They are not "less functional" at all.
It's true, not every language goes all-in on functional programming — but just because some functional languages are less strongly functional than others, that doesn't mean it makes sense to call every language currently in use a functional language.

I mean, it seems pretty useless to define "functional programming language" in a way that includes both C and OCaml just because you can pass functions around in both of them.

   var a = 1;
   a = 2;

   window.someGlobal = 'foo';

    const a = {b: "foo"};
    a.b = "bar";
Yea, const provides immutability with respect to assignment, but without immutable objects and collections, it isn't going to get you all the way.

    const a = icepick.freeze({b: "foo"});
Granted, that's not as elegant as built-in support but that is literally what I'm doing at the moment when I want immutability.
So if you go to extraordinary lengths you can use JavaScript functionally but it's not very functional by default aside from the first-class functions. Personally I just embrace it's imperative/functional blended nature and go with it.
Microsoft ruined C# by adding var. var is not your friend. Dynamic typing is not your friend and static languages pretending to be dynamic is akin to evil.
Implicit typing is not dynamic typing, or even "pretending to be dynamic". It's also, I think, off topic - as the example seemed to be talking about mutability in JS, with no C# for miles.
A functional language can allow mutations (see Haskell, LISP, etc.).

All a functional language really needs is first class functions. JS has had that since the very beginning.