Live data from Hacker News

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

news.ycombinator.com

11–13 of 13 posts

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

#11
FP is a way of thinking, which FP languages give nice features/syntax for. You can do non-FP style programming with an FP language.

In fact, many FP language features have been borrowed by non-FP languages to the point people don't consider them "FP" anymore[0]. (Like JS `.map()` and `.reduce()`. Even Excel macros now support first-class functions[1].)

Three features that would make FP thinking in JS easier:

1. immutable tuples and records[2]

2. pipe operator[3]

3. pattern matching[4]

Major epiphany: According to Grokking Simplicity[5], good functional programming isn't about avoiding impure functions; instead it's about giving extra care to them. Impure functions depend on the time they are called, so they are the most difficult to get right. In the end, the purpose of all software is to cause some type of mutation/effect (flip pixels on a screen, save bits to storage, send email, etc).

"Functional Core, Imperative Shell"[6] is probably the most useful FP concept. It is about pushing mutation/errors/impure functions to the outer edges of your code, so the inner code can focus on the "happy path." Complex business logic lives in the inner core; the outer shell is often simple/branchless so unit tests aren't even needed.

[0]: https://hw.leftium.com/#/item/21280429

[1]: https://hw.leftium.com/#/item/26900419

[2]: https://hw.leftium.com/#/item/23924933

[3]: https://hw.leftium.com/#/item/34454184

[4]: https://hw.leftium.com/#/item/16921652

[5]: https://www.manning.com/books/grokking-simplicity

[6]: https://hw.leftium.com/#/item/18043058

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

#12

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…

Remember you can mix FP with other paradigms.

For example, feel free to use mutation if it's limited to the implementation of a small function. The function will still be effectively pure (to callers of the function).

Or when implementing "functional core, imperative shell," Gary Bernhardt says he tends to start with imperative code in the shell, then refactor it into the functional core[1].

[1]: https://hw.leftium.com/#/item/34866473

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

#13

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…

Another example of this is referential equality testing. You know deeply nested data structures are the same just by comparing their memory addresses since they cannot change.

Someone was able speed up React's virtual DOM using Clojure data structures.

Post reply on HN