3 ms·
I realized this around 2016 while working in PHP/Laravel and realizing that mutation=state=time. I use higher-order methods like map/reduce/filter now, storing
by zackmorris 2y ago
I realized this around 2016 while working in PHP/Laravel and realizing that mutation=state=time.
I use higher-order methods like map/reduce/filter now, storing temporary results in variables when needed (although PHP's lack of const variables is a travesty). PHP's functional model of receiving data, transmuting it with the help of copy-on-write arrays, and returning it with a possible side effect stored in a database, all in an independent process, is one of the best around IMHO. It's too bad that legacy mistakes/features in the language and performance inefficiencies in operating system multiprocessing implementations (Windows, cough) obfuscate how powerful this model is.
Most/all of the complexity in debugging comes from mutation and side effects. Unfortunately nearly every language gets this wrong. Python has a huge emphasis on mutating variables rather than returning values that can be piped to the next function. Rust goes to great lengths with its borrow checker to allow mutation when copy-on-write would negate most of the need for that. I'd like to see a language and even an operating system that piped data around rather than mutating it in place, with an accepted cost of taking about twice as much memory to accomplish it. Then code could be mapped directly to spreadsheet form, and code execution could be mapped to diffs between state transitions to provide rewind functionality and debugging for free.
I would love to work on a paradigm like this if I had any spare money and time at all to do it. I've waited since the 90s for what I consider basic features, but sadly almost no language got this stuff right in the time since. Which leaves me with the unfortunate impression that probably 90% of the work we do in programming is a waste of time. Imagine if that wasted effort had gone towards solving foundational problems in programming for the benefit of all, instead of internet lottery winners just rolling their profits into their next venture.
- sdenton4 2y agoAre you proposing functional programming? There's plenty of examples of languages going that route (including languages with varying levels of compromise on 'pure' functionality for performance).
- throwitaway1123 2y ago> code execution could be mapped to diffs between state transitions to provide rewind functionality and debugging for free. Some of what you're saying reminds me of the JS library Immer. Using JS proxies (which can intercept and detect arbitrary object mutations), it allows you to produce a new state by mutating a "draft" copy of the current state. The produceWithPatches function provides you with arrays of patches and inverse patches that are essentially diffs between the old state and the new state. You can then apply these patches and inverse patches (using Immer's applyPatches function) to rewind the state backwards and forwards. Elm was one of the first languages (on the frontend at least) to pioneer this concept of "time travel debugging", which was then popularized by the Redux DevTools web extension.