The ideas behind making your function APIs pure, even if your implementation mutates things behind the scenes, is a good one. Reminds me of the old saying: If a bear mutates an array in a forest, but no one is around to see it, does it really matter?
In Haskell the ST monad allows one to implement this pattern with the type system guaranteeing your function is still pure (you can’t mutate the input, or return mutable output).
Good explanation but I feel it’s a bit beginner. Which is fine, this is good advice.
Would be interested if you write some more advanced tricks to save memory in Python.
Thanks! There are other articles which go into more library-specific details, as part of an ongoing series of articles: https://pythonspeed.com/datascience/
One of the core tenants of Clojure is that memory is either write once or fully thread safe with software transactional memory. It makes sense to let you operate both ways but force you to drop to Java if you’re going to do it without either.
It’s even better because you also have transients that allow doing local mutation, and the compiler can guarantee that it never escapes the scope of the function. And persistent data structures provide a really good middle ground between copying and mutating by revisioning it instead. This approach tends to be performant enough for vast majority of cases without incurring the danger of mutation.