[Haskell-cafe] minor refactoring problem

Martin DeMello martindemello at gmail.com
Tue Nov 29 09:16:58 CET 2011


I have the following functions:

makePair :: (String, String) -> IO PairBox

parseFile :: String -> [(String, String)]

importFile :: Editor -> String -> IO ()
importFile ed path = do
  s <- readFile path
  ps <- mapM (\x -> makePair (x, "")) (lines s)
  es <- return $ V.fromList ps
  writeIORef ed es

loadFile :: Editor -> String -> IO ()
loadFile ed path = do
  s <- readFile path
  ps <- mapM makePair (parseFile s)
  es <- return $ V.fromList ps
  writeIORef ed es

The problem is that loadFile and importFile are so similar it seems a
shame not to combine them somehow, but anything I can think of doing
leaves the code looking more rather than less messy. Any nice ideas?

martin



More information about the Haskell-Cafe mailing list