I start this question by framing my bigger problem.
I have several text files, each one representing text for a different language.
lang_en.txt, lang_es.txt, lang_fr.txt, lang_pt.txt, etc.
Each file is a list of key value pairs separated by a tab.
The files contain display strings for a GUI and are thus fairly small files.
I wish to read all the files into memory and close all the handles.
I suppose technically I could leave the file handles open as there are currently just 6 language files.
However, this just seems wrong to me, and I would really like to close the files and still have the data.
This is what I have tried
module Main
(main)
where
import System.IO
strictList (x:xs) = x `seq` x : strictList xs
strictList [] = []
main = do
h <- openFile "lang_en.txt" ReadMode
hSetBuffering h LineBuffering
content <- hGetContents h
let all_lines = strictList $ lines content
hClose h
putStrLn $ unlines all_lines
The above prints nothing.
Moving the hClose to the bottom will print the file contents.
So `seq` doesn't seem to be working.
I am using GHC version 7.6.3.
There is also a second part of this puzzle that I don't know how to do.
I desire to have something like
data LanguageKey = En | Es deriving (Ord, Eq)
data DisplayKey = Red | Green | Blue deriving (Ord, Eq)
en = Data.Map.fromList [(Red, "red"), (Green, "green"), (Blue, "blue")]
es = Data.Map.fromList [(Red, "rojo"), (Green, "verde"), (Blue, "azul")]
languages = Data.Map.fromList [(En, en), (Es, es)]
{- |get display string given a languageKey and a displayKey -}
get :: LanguageKey -> DisplayKey -> Maybe [Char]
get languageKey key = find $ Data.Map.lookup languageKey languages
where
find (Just map) = Data.Map.lookup key map
> get Es Red
Just "rojo"
The problem with this version of "get" is that the user will be able to change the language via a GUI.
Thus the languageKey will change at runtime.
I would like to avoid needing to pass languageKey into a very large number of functions.
And finally, I just thought of a third problem.
I know that lookup will always succeed in retrieving a value. A failure would indicate a bug in the text files.
Is there any way to have "get" return a String instead of Maybe String?