diff --git a/JFrame.hs b/JFrame.hs new file mode 100644 index 0000000000000000000000000000000000000000..b233909148fef18d5b78e5eec728bf92b075b69a --- /dev/null +++ b/JFrame.hs @@ -0,0 +1,23 @@ +type Frame a = [(String, a)] + +-- Create a new frame with an empty list of key-value pairs +newFrame :: Frame a +newFrame = [] + +-- Add a new key-value pair to the frame +addToFrame :: String -> a -> Frame a -> Frame a +addToFrame key value frame = (key, value) : frame + +-- Lookup the value associated with a key in the frame +lookupInFrame :: String -> Frame a -> Maybe a +lookupInFrame _ [] = Nothing +lookupInFrame key ((k,v):xs) + | key == k = Just v + | otherwise = lookupInFrame key xs + +-- Update the value associated with a key in the frame +updateInFrame :: String -> a -> Frame a -> Frame a +updateInFrame _ _ [] = [] +updateInFrame key value ((k,v):xs) + | key == k = (k, value) : xs + | otherwise = (k, v) : updateInFrame key value xs diff --git a/JHashMap.hs b/JHashMap.hs index 9ae75ab2bd6d68f94ca6f7661e1bc67ae1ac2186..fdca64d62ae37a536f9868791d10014ff808f541 100644 --- a/JHashMap.hs +++ b/JHashMap.hs @@ -1,14 +1,27 @@ import qualified Data.Map as Map import Data.IORef +type MutableMap a = IORef (Map.Map String a) + +newMap :: IO (MutableMap a) +newMap = newIORef Map.empty + +insertMap :: MutableMap a -> String -> a -> IO () +insertMap ref key value = modifyIORef' ref (Map.insert key value) + +lookupMap :: MutableMap a -> String -> IO (Maybe a) +lookupMap ref key = Map.lookup key <$> readIORef ref + main :: IO () main = do -- Create a new mutable map julioHashMap <- newMap -- insertMap will be called whenever an EqualsToken is located - insertMap julioHashMap "tile1" "valuesLoadedIn1" - insertMap julioHashMap "tile2" "42" + -- insertMap should be able to take in any value in the key-pair value + insertMap julioHashMap "tile2" (1 :: Int) + insertMap julioHashMap "tile1" "example" + -- When a variable name is seen and not an equals, use "lookupMap" @@ -19,15 +32,3 @@ main = do -- Temporary just to check if working putStrLn $ "tile1: " ++ show val1 putStrLn $ "tile2: " ++ show val2 - - -type MutableMap a = IORef (Map.Map String a) - -newMap :: IO (MutableMap a) -newMap = newIORef Map.empty - -insertMap :: MutableMap a -> String -> a -> IO () -insertMap ref key value = modifyIORef' ref (Map.insert key value) - -lookupMap :: MutableMap a -> String -> IO (Maybe a) -lookupMap ref key = Map.lookup key <$> readIORef ref