4 ms·
> Leftpad is an entirely pure, non-diverging function. `leftpad(string, int) -> string` needs to allocate so not fully pure that way. You could pass in the res
by klabb3 3y ago
> Leftpad is an entirely pure, non-diverging function.
`leftpad(string, int) -> string` needs to allocate so not fully pure that way. You could pass in the resulting string but then it would have to mutate, which is not really pure either.
- atlintots 3y agoIt would be pure in languages without manual memory management
- naasking 3y agoUnless it can't allocate, and then the whole program terminates. There are unfortunately limitations everywhere.
- Kinrany 3y agoIf the whole program terminates, the purity of the function is still intact, the computer just can't compute it :D
- metadat 3y agoIs it really worth worrying about? This strikes me as pedantic. Once a program can no longer allocate memory, it's very likely toast anyway. Edit: @naasking, all good points. Still, isn't the general strategy to ensure memory isn't exhausted, rather than handling such a situation "gracefully", whatever that means? :)
- naasking 3y agoIt depends what your program is for. Some programs need guarantees because of bounded resources, so you may want resource allocation to be a visible effect (like embedded systems), or because you're running potentially untrusted code (like JS or WASM in a browser), or because you want sane failure handling (like running modules in a web server).
- arzig 3y agoGraceful is a misnomer, but there are better versions. If you are day, a database server, and you run out of memory and die horribly, availability for all clients is compromised. If you begin rejecting queries or connections until memory js available at least some availability is maintained.
- metadat 3y agoI don't follow, it seems like you're still be hosed in this scenario. What's the difference of stopping accepting connections and rejecting queries vs crashing out? Meaningful work cannot make progress when a busy dynamic system is OOM -- which a database is a prime example of. Best to avoid the condition, or design the client side to handle the possibility the resource could be unavailable.
- tialaramex 3y agoOf course as well as limiting the total amount of memory a running program is allowed to allocate, we can also limit how many CPU instructions it can execute and numerous other (OS dependent) resource uses. Accounting for this all operations are fallible, which no practical language copes with. It's very normal in a Unix to be allowed to limit total runtime for example.
- amluto 3y agoIt’s certainly pure in the Haskell sense: the inputs are two immutable values, and the output is another immutable value. It’s even pure in some Rusty senses: it could take String or &str as input and returns a String — in a model where String is a value, it’s pure. A lot of modern programming logically involves telling a computer to what to compute and not particularly caring how it gets computed. Yet we mostly lack a language in which one can specify computations and then then run the result portably and safely.