[Haskell-beginners] Working With TVars

aditya siram aditya.siram at gmail.com
Tue Apr 28 01:43:36 EDT 2009


Hi all,
I have a tuple inside a TVar :
> type MySTM = TVar (Int,Int)

Whenever I want to edit/read 'a' or 'b' I find myself writing :
> editFunction mySTM = do
(a',b') <- readTVar mySTM
dostuff a'
...

This is boilerplate stuff, so I decided to write some accessors. So far I
have :
> getA , getB :: MySTM -> STM Int
> getA mySTM = do
> (a',b') <- readTVar mySTM
> return a'
>
> getB mySTM = do
> (a',b') <- readTVar mySTM
> return b'

I want to be able to use these accessors like so:
> doSomethingWithA mySTM = do
> case (getA mySTM) of
>    1 -> doStuff
>    0 -> doSomethingElse

But getA returns an STM Int, so I still have to do a :
> doSomethingWithA = do
> a' <- (getA mySTM)
> case a' of
>    1 -> doStuff
>    0 -> doSomethingElse

This doesn't really save me a lot of boilerplate. What is the best way of
writing a function that just returns my values do I can work with them in
the STM monad without unpacking them all the time?

Thanks ,
Deech
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20090428/4d57a761/attachment.htm


More information about the Beginners mailing list