Home > other >  Getting IO based on the contents of an IO stream
Getting IO based on the contents of an IO stream

Time:02-20

I have a situation where I am trying to concatenate the contents of two text files, A and B. The complication is that the location of B is specified in the contents of A. I've created a function (minimal example below), which reads A, opens B, and then just tries to stick them both together, but frankly this method seems too easy to be correct and I have a feeling that it may not be the best approach. It compiles but I'm unable to test it since it can't find the second file (presumably something to do with paths, but I've not yet figured out what). Any advice appreciated.

getIOFromIO :: IO String -> IO String
getIOFromIO orig = do
     origContents <- orig
     moreIO <- readFile origContents
     return (origContents    " "    moreIO)

CodePudding user response:

The function getIOFromIO should work fine, provided you pass it an IO action that reads the first file, like:

getIOFromIO (readFile "foo.tmp")

and provided the entire contents of foo.tmp, including any preceding or trailing whitespace (like a trailing newline) are part of the desired filename.

The following self-contained example demonstrates its use:

setup :: String -> String -> IO ()
setup file1 file2 = do
  writeFile file1 file2 -- put name of file2 in file1
  writeFile file2 $ "body\n"

-- unmodified from your question
getIOFromIO :: IO String -> IO String
getIOFromIO orig = do
  origContents <- orig
  moreIO <- readFile origContents
  return (origContents    " "    moreIO)

main = do
  setup "foo.tmp" "bar.tmp"
  txt <- getIOFromIO (readFile "foo.tmp")
  print txt

It should generate the output:

"bar.tmp body\n"
 ^^^^^^^ ^^^^
       |    ` contents of second file (bar.tmp)
       |
        `- contents of first file (foo.tmp)
  • Related