4 ms·
I won’t disagree that JavaScript isn’t multi-paradigm and you can write in a functional style, but you have to look at where the ergonomics are: • dot, `.`, sy
by toastal 4y ago
I won’t disagree that JavaScript isn’t multi-paradigm and you can write in a functional style, but you have to look at where the ergonomics are:
• dot, `.`, syntax on objects vs. composition/pipe operators
• no autocurry
• no ADTs or basic pattern matching
• has null + undefined but no Maybe/Option (there is null coalescing and null chaining, but it’s easy to accidentally skip)
• const can still mutate the on the object, freeze incurs a massive performance cost
• no tail-call optimization outside Safari despite being in the ECMA2015 spec
• `class` keyword existed and private methods and values now, FP is pretty anti-encapsulation
• no immutable data structures in the stdlib
• no way to prevent side effects (/* __PURE__ */ in code generation is a hack)
Can you get benefits from avoiding state/mutation and choosing composition over inheritance? Sure, but it’s nothing like being in a language where these aren’t something you can do or the default, but the only way to write code and having the tool to express it.
- agentultra 4y agoIt's a mess for sure and you'll never get a reasonable FP language from it. You won't get a Smalltalk out of it either. Instead we get a mish-mash of ideas hashed together by a committee of people with differing backgrounds, experiences, and ideas. ... but some of those FP ideas are pretty neat and when used well can make code that is easier to maintain (for people who are familiar with the concepts).
- rileyphone 4y agoIt's actually fairly easy to get most of a Smalltalk out of Javascript. Prototypes and first-class functions form a really powerful basis for a higher language. Though, to your point, it wouldn't really still be Javascript.
- rory 4y agoDo you know of an example snippet / article demonstrating how to do this?
- rileyphone 4y agoSmalltalk in Self: nearly complete Smalltalk implementation on similar prototypical language. http://www.merlintec.com/download/mario.pdf http://www.merlintec.com/download/mario.pdf Mootools: ancient class system for Javascript. https://mootools.net/core https://mootools.net/core Also, here's a very old Crockford article on implementing classes in Javascript. https://crockford.com/javascript/inheritance.html https://crockford.com/javascript/inheritance.html
- rory 4y agoThank you!
- wiseowise 4y agoYou’re implying that those things are necessary to write reasonable functional programs. They’re not.
- agentultra 4y agoTrue. Reasonable to me is a pretty high bar. You can write in a functional style in JS and feel it is reasonable. You might feel differently after using SML/OCaml/Haskell/etc.