[Haskell-cafe] minor refactoring problem

Holger Siegel holgersiegel74 at yahoo.de
Tue Nov 29 11:29:16 CET 2011


Am 29.11.2011 um 09:16 schrieb Martin DeMello:

> 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?


fromRawFile, fromSavedFile :: String -> IO [PairBox]
fromRawFile = mapM (\x -> makePair (x, "")) . lines
fromSavedFile = mapM makePair . parseFile

setEditor :: Editor -> [PairBox] -> IO ()
setEditor ed = writeIORef ed . V.fromList

importFile, loadFile :: Editor -> String -> IO ()
importFile ed = readfile >=> fromRawFile >=> setEditor
loadFile ed = readfile >=> fromSavedFile >=> setEditor





More information about the Haskell-Cafe mailing list