From 67ec309eefc395ee15eb742f193f5d5ae5142b6c Mon Sep 17 00:00:00 2001 From: Paul3103 <plw1g21@soton.ac.uk> Date: Thu, 20 Apr 2023 16:15:30 +0100 Subject: [PATCH] Added a frame haskell file just to see if it looks more suitable than the hashmap --- JFrame.hs | 23 +++++++++++++++++++++++ JHashMap.hs | 29 +++++++++++++++-------------- 2 files changed, 38 insertions(+), 14 deletions(-) create mode 100644 JFrame.hs diff --git a/JFrame.hs b/JFrame.hs new file mode 100644 index 0000000..b233909 --- /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 9ae75ab..fdca64d 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 -- GitLab