3 ms·
You... don't. You have to rely on compiler optimizations to get good performance. Monads are more-or-less syntax sugar. They give you a structure that allows t
by lieks 2y ago
You... don't. You have to rely on compiler optimizations to get good performance.
Monads are more-or-less syntax sugar. They give you a structure that allows these optimizations more easily, and also make the code more readable sometimes.
But in your example, update returns a new copy of the state, and you map it over a list for each step. The compiler tries to optimize that into in-place mutation.
IMO, having to rely so much on optimization is one of the weak points of the language.
- kreetx 2y agoYou do, and you'll have to use do destructive updates within either ST or IO monad using their respective single variable or array types. It looks roundabouty, but does do the thing you want and it is fast. ST and IO are "libraries" though, in the sense that they not special parts of the language, but appear like any other types.
- whateveracct 2y agoFast immutable data structures don't rely on compiler optimizations. They just exist lol.