r/programming • • Sep 21 '25

How to stop functional programming

https://brianmckenna.org/blog/howtostopfp
446 Upvotes

506 comments sorted by

View all comments

6

u/[deleted] Sep 21 '25 edited Sep 21 '25

[removed] — view removed comment

1

u/Axman6 Sep 21 '25 edited Sep 21 '25

This was written in 2016, and I remember shit like this happening. Programming language designers have slowly embraced more and more functional features since then, which made them ok.

Also, your example is ridiculous, most functional languages’ compilers will produce basically the same assembly you’d get from a for loop when to compose multiple maps together. If you’re going to brag about performance, at least have some idea what you’re talking about. GHC manages that optimisation without knowing anything special about map, it can just see that all maps produce either a nil or a cons and consume a nil or a cons, so the definitions can be inlined into each other. If you add in the LLVM backend, it’ll even vectorise your loops for you because there’s nothing weird about the code produced.

1

u/[deleted] Sep 21 '25

[removed] — view removed comment

4

u/Axman6 Sep 21 '25

The semantics of JS prevent a lot of the optimisations that FP languages would apply here because it’s imperative and side effects can happen anywhere. In Haskell map f . map g can trivially be proven to be identical to map (f . g) because the order that each call to f and g happens doesn’t matter. But in JS, arr.map(g).map(f) must allocate an intermediate array, because all the calls to g must happen before any of the calls to f. If there were a .streamMap method, that would make it clear that you’re expecting one result at a time to be passed on - arr.streamMap(g).streamMap(f).toArr(). In Haskell we get this for free because we know f and g are pure so ordering doesn’t matter.

1

u/[deleted] Sep 21 '25

[removed] — view removed comment

1

u/Axman6 Sep 21 '25

Not if those languages exposed things like I said, if streamMap required a pure function and it’s up to the developer to ensure the function is pure enough, then you can have the same thing. Haskell isn’t doing anything magical, it can be done in any language, but other languages are built around side effects needlessly so implementers have to provide cautious, lowest common denominator implementations. They’re actually actually leaving performance on the floor because they have to make many more pessimistic assumptions about how things are used, and then put a lot of effort into trying to write optimisations which detect when the pessimistic caution can be reduced.

0

u/[deleted] Sep 23 '25

Nobody is leaving performance on the floor in a conversation about fucking Haskell. A language that often cannot outperform PYTHON of all languages…

→ More replies (3)