3 ms·
I wish people would stop being such zealots when talking about functional programming. JS is mostly an imperative language, with OO and FP features, that you ca
by Zababa 5y ago
I wish people would stop being such zealots when talking about functional programming. JS is mostly an imperative language, with OO and FP features, that you can use when they make sense. From the article you linked:
> This is a function but it is not functional because it mutates state and uses iteration.
The state and iteration was only internal, JS doesn't support TCO and the algorithm is very simple. Sure it's only an example but it's not a great one. Then it continues:
> To be functional it is not sufficient to simply remove classes and write top-level functions. You have to completely change your way of thinking. The functional way has no side effects or state mutations. It has no assignments, no iteration, and no conditional statements.
That's not "being functional", that's Haskell. I can write a program with side effects, state mutations, assignements, iterations and conditional statements in OCaml or Scala without issues.
> Assignment and iteration are typical of imperative programming. In functional programming changing the value of a variable using an assignment statement is not allowed.
It is allowed though:
let x = ref 0;
x := 5 (* x is now 5! *)
Again, that vision of "functional programming" is "strictly Haskell".
> This is because JavaScript lacks a critical feature known as lazy evaluation. Lazy evaluation means not calculating things until they are actually needed.
JS has lazy evaluation with iterators. What it doesn't have is chaining/transformation of those iterators: map, filter, reduce are not defined for iterators. You can make your own prime iterator (with internal state!) and iterate on it though.
> Because of a lack of features such as tail call optimization and lazy evaluation you can’t do “real” functional programming in JavaScript.
No true scottsman fallacy. By the way, Safari implements TCO, so you should be able to do real functional programming on Safari with iterators!
> Functional programming and side effects
Again, that's only for Haskell. And you can write scary imperative code in Haskell too:
toto x = if x == 0 then error "Ooops" else x + 3
This will add 3 to any integer, except 0, in which case it will raise an exception. This is nowhere in the type of toto. Also, all that mention of "pure and impure" programming and no mentions of monads and promises, which are almost just like monads?
> Among some JavaScript programmers, it has become fashionable to completely avoid classes and OOP because they think functional is better. But given that you can’t do real functional programming in JavaScript, completely avoiding OOP will only eliminate the advantages of OOP while not gaining the benefits of pure functional style.
False dichotomy, you can write plain imperative code in JS, and it works well.
> That’s not to say there are no benefits at all to incorporating some functional style into JavaScript code, but wholesale replacement of OOP with functional style is not warranted.
Again, most existing JS code is imperative style and not OO. No mention how functional code is easier to test? That's a bit weird.
All in all, it's a bad article based on limited views.
- petilon 5y ago> I can write a program with side effects, state mutations, assignments, iterations and conditional statements in OCaml or Scala without issues. That's only because those languages are not purely functional. If you have state mutations and side effects you're not doing FP at all. Just because a language is known as a "functional language" doesn't mean anything and everything you are able to do in that language qualifies as FP.
- Zababa 5y agoYou're mixing function programming and pure functional programming. > That's only because those languages are not purely functional. If you have state mutations and side effects you're not doing FP at all. You can be doing functional programming with state and mutations. Functional programming is about having first-class functions and using them, and often takes the form of function composition and higher-order functions. > Just because a language is known as a "functional language" doesn't mean anything and everything you are able to do in that language qualifies as FP. That is true. But you can have an impure codebase and still be doing FP without issues.
- petilon 5y ago> You're mixing function programming and pure functional programming. I am glad you said "function programming", as in, programming with functions. That's what you're doing. Don't confuse it with Functional Programming!
- Zababa 5y agoThat was a typo. Please define functional programming, as every source I've seen define it as precisely that, programming with functions.