Skip to content

Instantly share code, notes, and snippets.

@viercc
Last active March 30, 2020 08:41
Show Gist options
  • Save viercc/2840a67ca409ebfdce203c23706c9eed to your computer and use it in GitHub Desktop.
Save viercc/2840a67ca409ebfdce203c23706c9eed to your computer and use it in GitHub Desktop.
On hashtables with "undo-able" insert
-- Just wrote down in the web editor.
-- Not tested at all but I hope you get the idea
import qualified Data.HashTable.ST.Basic as HT
import Data.List.NonEmpty(NonEmpty(..))
import qualified Data.List.NonEmpty as NE
type MyHashTable s k v = HT.HashTable s k (NonEmpty v)
insert :: MyHashTable s k v -> k -> v -> ST s ()
insert hashTable k v =
HT.mutate hashTable k $ \prev ->
(Just (v :| maybe [] NE.toList prev), ())
update :: MyHashTable s k v -> k -> v -> ST s ()
update hashTable k v = HT.insert hashTable k (v :| [])
pop :: MyHashTable s k v -> k -> ST s (Maybe v)
pop hashTable k =
HT.mutate hashTable k $ \prev ->
case prev of
Nothing -> (Nothing, Nothing)
Just (v :| rest) -> (NE.nonEmpty rest, Just v)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment