ArrayRef-0.1: Examples/SyntaxSugar.hs
-- This module demonstrates using of syntax sugar when manipulating
-- references, arrays and hash tables, including implementation
-- of monad-independent algorithms with references
import Control.Monad.ST
import Data.SyntaxSugar
import Data.Ref
import Data.ArrayBZ.IO
import Data.HashTable as Hash
main = do
-- This section demonstrates running of monad-independent
-- algorithm that use boxed references in IO and ST monads
test_Ref 5 >>= print
runST (test_Ref 6) |> print
-- This section demonstrates running of monad-independent
-- algorithm that use unboxed references in IO and ST monads
test_URef 7 >>= print
runST (test_URef 8) |> print
-- Using syntax sugar with arrays
test_Array >>= print
-- Using syntax sugar with hash tables
test_Hash >>= print
-- Monad-independent computation that uses boxed reference
test_Ref init = do
x <- ref (0::Int)
x =: init
x += 10
x -= 2
x .= (*2)
val x
-- Monad-independent computation that uses unboxed reference
test_URef init = do
x <- uref (0::Int)
x =: init
x += 10
x -= 2
x .= (*2)
val x
-- Using syntax sugar with arrays
test_Array = do
let i = 0::Int
arr <- newArray (0,9) 0 :: IO (IOArray Int Double)
(arr,i) =: 1
(arr,i) += 10
(arr,i) -= 2
(arr,i) .= (*2)
x <- val (arr,i)
arr2 <- newArray ((0,0), (9,9)) 0 :: IO (IOArray (Int,Int) Double)
(arr2,i,i) =: 1
(arr2,i,i) += 10
(arr2,i,i) -= 2
(arr2,i,i) .= (*2)
y <- val (arr2,i,i)
return (x,y)
-- Using syntax sugar with hash tables
test_Hash = do
let key = "test"
hash <- Hash.new (==) hashString
(hash,key) =: 1
(hash,key) += 10
(hash,key) -= 2
(hash,key) .= (*2)
val (hash,key)
-- Helper operations
a |> b = b a
for list action = mapM_ action list