Live data from Hacker News

Ask HN: What do FP languages have that JavaScript doesn't?

news.ycombinator.com

1–10 of 13 posts

Ask HN: What do FP languages have that JavaScript doesn't?

#1
Whenever I read a functional programming tutorial or course, it always sings the praises of how there's an amazing REPL you can use to experiment with your code, or it will teach how to map and reduce over sequences, or first-class functions, or currying.

The problem for me is, all of these awesome features already exist in popular languages like JS and Python.

Then there's the "purity" side of things. For e.g. tutorials for Haskell and Clojure say that "mutation and side effects are not allowed" which makes for a more predictable program. But in my experience, I can totally just re-define variables in the REPL and no one stops me. I don't even get a warning. So I'm not sure if I'm missing something, but so far it doesn't feel so different from writing fancy ES6 syntax.

The last thing is "macros". This one seems to be actually unique to FP languages like Clojure, etc. But is this the only unique feature of FP languages?

Genuinely curious, please help me clear up my understanding. I have so far been following some fun Clojure and Haskell tutorials and these languages are very fun/sleek/logical to write in.

I am just wondering if I could accomplish the same in JS easily without learning whole new languages and ecosystems.

Re: Ask HN: What do FP languages have that JavaScript doesn't?

#2
Most likely you know that Functional Programming (FP) is a Programming paradigm, that is, an approach to write the programs you have in mind. You have already mentioned some features of such paradigm, I am revisiting JavaScript myself (Since it was a long time since I use it) these days. And I am glad to find famous functions associated with FP, like flatMap, already available in JS. Since you already mentioned lots of these features, the only one that comes to my mind is Lazy Evaluation, which gives you the ability to work with "Infinite structures".

Perhaps you are already aware of these resources, but I will never get tired of recommending the following: * FP101x by Mr. Erik Meijer (The course is archived on edx.org but you can work your way through it). * The above is based on the outstanding book by Graham Hutton, Programming in Haskell. * And, if you can afford it in terms of time, a book from Mr. Peter Van Roy, Concepts, Techniques, and Models of Computer Programming. (There is a section devoted to FP).

Last but not least, I do love JavaScript.

Hope that helps.

Re: Ask HN: What do FP languages have that JavaScript doesn't?

#5

Most likely you know that Functional Programming (FP) is a Programming paradigm, that is, an approach to write the programs you have in mind. You have already mentioned some features of such paradigm, I am revisiting JavaScript myself (Since it was a long time since I use it) these days. And I am glad to find famous functions associated with FP, like flatMap, already available in JS. Since you already mentioned lots…

Yes lazy evaluation is a good one. I believe Python does have that with its generators/yield syntax, but I found lazy evaluation simpler to work with in Haskell

Re: Ask HN: What do FP languages have that JavaScript doesn't?

#7
One easy advantage is predictability due to thing not changing. Let's say there is an array (maybe I should have written list). Since that array cannot change in fp, when you do operations with that array, you don't have to worry it changed.

Then the language itself will also know this and manage memory accordingly.

So if you have array with 1,2,3 and an array with 3,4,5 if you unite the arrays memory can just reference the two array as if they were one instead of creating a new one, because for sure they will never change. Imagine this on very big arrays

Re: Ask HN: What do FP languages have that JavaScript doesn't?

#8
Functional Programming (FP) can be extremely elegant, and you should do your best to at least get familiar with it to see how using it can improve your code quality. There's a lot of theoretical reasons why this paradigm might be particularly great in a future where all devices seem to be adding lots of processing cores. For instance, without a function having side-effects, the potential for conflicts between threads or processes could be completely eliminated, so FP can potentially have some real-world benefits beyond just more elegant code.

That said, you should also think in terms of "What does JavaScript have that functional programming doesn't?". As a working-stiff programmer, most of the time the problems you're going to encounter are going to be a lot easier and faster to solve if mutating state is available as an option.

Re: Ask HN: What do FP languages have that JavaScript doesn't?

#9

One easy advantage is predictability due to thing not changing. Let's say there is an array (maybe I should have written list). Since that array cannot change in fp, when you do operations with that array, you don't have to worry it changed. Then the language itself will also know this and manage memory accordingly. So if you have array with 1,2,3 and an array with 3,4,5 if you unite the arrays memory can just refere…

That's a really cool example of how it can work behind the scenes, thanks

Re: Ask HN: What do FP languages have that JavaScript doesn't?

#10

Functional Programming (FP) can be extremely elegant, and you should do your best to at least get familiar with it to see how using it can improve your code quality. There's a lot of theoretical reasons why this paradigm might be particularly great in a future where all devices seem to be adding lots of processing cores. For instance, without a function having side-effects, the potential for conflicts between threads…

That's true, concurrency is becoming an important consideration and is probably why predictable and FP-oriented langs like Elixir, Rust, Clojure are having good growth.

As for mutating state, I come from an embedded software (microcontrollers) background haha and there's no option but to mutate state

Post reply on HN