4 ms·
JavaScript lends itself really well to functional programming, no?
by Waterluvian 10y ago
JavaScript lends itself really well to functional programming, no?
- wereHamster 10y agoNot really, no. Example: partial function application does not feel nearly as natural as in in Haskell.
- altcognito 10y agoThe reason they use JavaScript is because it's known by many and the examples can be tried without downloading/having an additional compiler. There are at least two dozen concepts on the list there - the value of illustrating new concepts for some with a known quantity far outweighs the downsides of having to start with a fresh environment. I will say though for the exact cases you mention, they probably should be clear why this isn't exactly the concept as described.
- gaius 10y agoHere you go: http://tryhaskell.org http://tryhaskell.org
- Waterluvian 10y agoIt's definitely no Haskell. But if you wanted to do FP in JavaScript, it wouldn't be a painful ordeal, no? Well, ES6 and newer at least.
- catnaroek 10y agoFunctional programming puts the static structure of a program at the forefront: types, invariants, equations relating inputs and outputs of processes, etc. JavaScript doesn't do this very well. In fact, even C++ does it better than JavaScript. Nobody would in their reasonable mind call C++ a functional language, right?
- justinhj 10y agoIn Javascript functions are first class objects, there are closures, and higher order functions. Having a garbage collector makes it easier to implement persistent data structures. I've always thought that despite it's weird corners it's actually a powerful and flexible language. You can see that in the article, because in just a handful of lines of code it is able to express all of these ideas in an accessible way. I'd be interested to see a C++ version of this.
- catnaroek 10y ago> functions are first class objects Programming languages have procedures, not functions. (Yes, even Haskell. Consider the possibility of divergence.) Functional programming is a style that: (0) Emphasizes values over physical object identities. (1) Emphasizes procedures that compute functions - mappings from values from a domain, into values from a codomain. (2) Discourages distinguishing between procedures that compute the same function. Functional languages are languages that facilitate and encourage functional programming. Does JavaScript? IMO, it doesn't even do (0) very well, which is a precondition for discussing whether it does (1) or (2) well. > garbage collector, persistent data structures Don't conflate “purely functional” with “persistent”. Purely functional programming can deal with ephemeral data structures just fine - using substructural types. In the other direction, there exist persistent data structures whose implementation internally uses imperative assignment. > powerful and flexible Those are desirable qualities in a language, but they are orthogonal to whether the language supports functional programming. > express all of these ideas in an accessible way JavaScript doesn't even make it easy to talk about structurally equal values. Since everything is mutable, the only notion of equality that the language will actually respect is equality of physical object identities. Without the ability to talk (soundly) about structural value equality, you can't formulate algebraic laws (e.g., the monoid or group laws), which is the basis for equational reasoning - one of the key benefits of programming in a functional style!
- drewgross 10y agoYou can have weakly/dynamically typed functional languages (most lisps for example) just like you can have strongly/statically typed procedural or OO languages, like Fortran or C++. Haskell the language also knows nothing at all about invariants, and the language will happily let you make something a Monad even if it doesn't obey the right laws. The culture and libraries of course take invariants quite seriously, but some aspects of the Javascript culture also take invariants quite seriously, like Promises for example.
- keithnz 10y agoyes, I think it does, it's not a pure functional language so you can break out of functional programming easily ( and accidentally ). But nothing really stops you from doing FP or playing with FP concepts. There's some nice core FP libraries like Ramda for JS. Also we are seeing more and more application libraries that are taking a more functional approach. Could they have used a specific functional programming language? sure... But, I think that might of missed the target audience. Most haskell / lisp / Ocaml / etc etc programmer would be comfortable with these terms already.
- palunon 10y agoOnce you get far enough with template metaprogramming, C++ start to look like a slow version of Haskell with horrible syntax and cryptic error messages...
- DeadReckoning 10y agoIt is by far the best mainstream language for functional programming.