3 ms·
Because 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
by lostcolony 5y ago
Because 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.