Skip to content
Snippets Groups Projects
Commit 67ec309e authored by plw1g21's avatar plw1g21
Browse files

Added a frame haskell file just to see if it looks more suitable than

the hashmap
parent 6e7bfef2
No related branches found
No related tags found
No related merge requests found
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
import qualified Data.Map as Map import qualified Data.Map as Map
import Data.IORef 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 :: IO ()
main = do main = do
-- Create a new mutable map -- Create a new mutable map
julioHashMap <- newMap julioHashMap <- newMap
-- insertMap will be called whenever an EqualsToken is located -- insertMap will be called whenever an EqualsToken is located
insertMap julioHashMap "tile1" "valuesLoadedIn1" -- insertMap should be able to take in any value in the key-pair value
insertMap julioHashMap "tile2" "42" insertMap julioHashMap "tile2" (1 :: Int)
insertMap julioHashMap "tile1" "example"
-- When a variable name is seen and not an equals, use "lookupMap" -- When a variable name is seen and not an equals, use "lookupMap"
...@@ -19,15 +32,3 @@ main = do ...@@ -19,15 +32,3 @@ main = do
-- Temporary just to check if working -- Temporary just to check if working
putStrLn $ "tile1: " ++ show val1 putStrLn $ "tile1: " ++ show val1
putStrLn $ "tile2: " ++ show val2 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment