5 ms·
The Haskell Data.Array.Diff module provides arrays that use mutation behind the scenes, so that updates are O(1), but provide a pure functional interface. So i
by sethg 18y ago
The Haskell Data.Array.Diff module provides arrays that use mutation behind the scenes, so that updates are O(1), but provide a pure functional interface. So if "c" is the 256-element array of byte counts, and "i" is the index of the element you want to increment, then you can do this:
let c' = c // [(i, c!i + 1)]
To quote the documentation: "When the // operator is applied to a diff array, its contents are physically updated in place. The old array silently changes its representation without changing the visible behavior: it stores a link to the new current array along with the difference to be applied to get the old contents."
For more information: http://www.haskell.org/ghc/dist/current/docs/libraries/array/Data-Array-Diff.html http://www.haskell.org/ghc/dist/current/docs/libraries/array...
I don't know if something similar exists for Erlang, but if not, I assume it wouldn't be terribly hard to implement.
- old-gregg 17y agoCan you implement your own Data.Array.Diff in Haskell or it's just a standardized fallback to C? I'm on chapter 6 of 'Real World Haskell' and I honestly don't know yet.
- sethg 17y agoIt appears, from skimming the source code, that they use "unsafePerformIO", which is Haskell's "I want to trick the compiler into thinking that this imperative code is pure" function. (It's not part of standard Haskell but I think all implementations provide it.) You can use unsafePerformIO in your own code if you really need it--it's basically a giant loophole in the type system, so if you don't know what you're doing, you can screw yourself, but if you really need that power, it's there. More here: http://www.haskell.org/haskellwiki/IO_inside#Dark_side_of_IO_monad http://www.haskell.org/haskellwiki/IO_inside#Dark_side_of_IO...