I have two functions:
trans_table :: [Char] -> [Char] -> Map.Map Char Char trans_table s1 s2 = Map.fromList (zip s1 s2) random_trans_table :: IO (Map.Map Char Char) random_trans_table = do rawKey <- shuffle ascii let zipped = zip ascii rawKey let map = Map.fromList zipped return map
The first of these creates a two-line map; the second generates a random card. The first returns Map.Map Char Char; the second returns IO (Map.Map Char Char)
Now I need to look up the value from this Card, and I created two functions: one for the I / O card and one for the card:
translate_char :: Map.Map Char Char -> Char -> Maybe Char translate_char table c = Map.lookup c table translate_char_io :: IO (Map.Map Char Char) -> Char -> IO (Maybe Char) translate_char_io table c = do raw_table <- table let result = Map.lookup c raw_table return result
I don't like this because it causes code duplication. I already duplicate one function, and if I code in this way, I will need to duplicate all my functions.
Is there a way to create a function that will work with Map and IO (Map) the same way?
source share