packages feed

ArrayRef-0.1: Examples/URef.hs

-- This program demonstrates using of fast unboxed references in IO and ST monads
import Control.Monad.ST
import Data.Ref
import Data.Ref.LazyST ()

main = do
          -- IOURef is fast unboxed reference in IO monad
          test_IOURef 1         >>= print

          -- STURef is fast unboxed reference in ST monad
          runST (test_STURef 2) |>  print


-- Using 'IOURef Int'
test_IOURef init = do
          x <- newIOURef (init::Int)
          x0 <- readIOURef x
          writeIOURef x (x0+10)
          readIOURef x

-- Using 'STURef Int'
test_STURef init = do
          x <- newSTURef (init::Int)
          x0 <- readSTURef x
          writeSTURef x (x0+10)
          readSTURef x

-- Helper operation
a |> b = b a