3 ms·
"Meanwhile there's practically no tradeoff to writing functional code in an imperative language." I would have to disagree with you there. It partly depends on
by lostcolony 5y ago
"Meanwhile there's practically no tradeoff to writing functional code in an imperative language."
I would have to disagree with you there. It partly depends on what you mean, but I quickly give up on functional solutions in, say, Javascript (despite Javascript technically having all the bits I'd need!) just because of the lack of immutable data structures and cheap copying. Not to mention the constant pain point of asking "does this modify in place, or does it return a new" when dealing even with the standard library, let alone -user- code. I either need a library like Immutable.js, or I need to jump through hoops that are both not ergonomic, AND have performance penalties, or I embrace mutation (and at that point frequently rule out a fully recursive approach because the bookkeeping involved far outweighs any benefit).
- macintux 5y agoAgreed. Constraints (primarily in this context immutability) are invaluable. Writing functional, immutable style in Python is handy but can be maddening when you never know what to expect from libraries.
- eurasiantiger 5y ago> Not to mention the constant pain point of asking "does this modify in place, or does it return a new" when dealing even with the standard library, let alone -user- code I don’t see how this is a pain point, JS is pass-by-value for primitives and pass-by-reference objects. Ergonomics, sure, but isn’t the lack of pattern matching a much bigger obstacle the lack of immutable semantic sugar? Immutability is easy to gain, though, just make your variable object writable: false.
- lostcolony 5y agoBecause any recursive algorithm that needs to backtrack requires special care. That is, let's say I have a sodoku solver. I write a recursive solution wherein I guess the next value on the sudoku board based on the constraints. When an execution path completes and fails to be solved; my board is now populated with that 'bad' state, and I've lost the state I was in prior to the recursive call, at each level of recursion. I either need to make copies at every step (very unwieldy and inefficient), or explicitly write the 'undo' mechanism for each step. It's doable, but it's extra bookkeeping and logic to keep track of. Whereas immutable data structures give me an ergonomic and reasonably efficient way to maintain the prior state, and to create the next state. And, the mix of mutation and non-mutation constantly messes with me. Array.concat returns a new array, but Array.push() modifies the array in place. Array.slice returns a new array; Array.splice modifies the array in place. This -constantly- messes with me. The lack of a feature is just inconvenience that I work around. Having a mix of modalities causes me to trip over myself constantly.
- eurasiantiger 5y agoEasy solution: don’t use them, use destructuring. // arr.push(item); // return arr; return […arr, item];
- lostcolony 5y agoThat's a slightly more wieldy way of creating a copy of the array. So still has the inefficiencies. It also is only a syntactical help with adding things onto an array; it doesn't help with removing items.
- nwmcsween 5y agoWhy would you worry about low level details for a high level language, optimization is an implementation detail. Javascript in my opinion lends itself functional style programming
- scns 5y agoBecause he cares about his users who might be using his sofware on a smartphone? If you disregard performance completely, your users will either use your app because they have to and hate it or switch to something else if they can.
- tephra 5y agoYou also have to focus on the correct performance issues and personally I've never seen writing functional js/ts being the issue.
- lostcolony 5y agoDefine functional js/ts. If it's just "oh, hey, I am using first class functions everywhere" - yes, Javascript is great at that. If it's "I'm writing a bunch of recursive solutions for naturally recursive problems", Javascript is a pretty poor fit for it. You can do it, but it requires extra bookkeeping and care, as I mention on another comment.
- scns 5y agoI once benchmarked a simple loop, and the same loop done via recursion. The recursive solution was an order of magnitude slower.