surprisingly difficult: a shared vector in haskell
While the final solution will be in Go, I enjoy exploring prototyping in haskell as it often shows me errors in my thought process. This time I ended up wrestling with haskell support for building a shared data structure that is safe to use concurrently. There are some interesting discussions on the web on this specific topic, namely this stackoverflow discussion.
Assuming the data structure I want to model is a Vector of Ints, I opted to combine a channel of Ints with an STM (TVar) instance of the Vector. STM would provide for access to the Vector to be done properly anywhere in the code, but I found it easier to isolate modification of the Vector to one function, and using a chan to communicate with the function. The solution is below.gist
last update 2012-10-08