Data.Text.IO provides text-oriented file operations such as readFile and writeFile. A CSV writer can use it to send assembled Text directly to the filesystem instead of converting through String. section 8.3
Writing CSV text
Convert CSV columns into rows, transform fields to Text, join fields with commas and rows with newlines, then compose the result with TIO.writeFile. section 8.3
Listing 8.8 Functions to write CSV values to files
toFileContent :: Csv -> [T.Text]
toFileContent Csv {..} =
let rows = L.map (L.map dataFieldToText)
$ L.transpose csvColumns #1
in L.map (T.intercalate ",") $ #2
M.maybe rows (: rows) csvHeader #3
writeCsv :: FilePath -> Csv -> IO ()
writeCsv path = TIO.writeFile path .
T.intercalate "\n" . toFileContent #4
Inspect the complete writeCsv pipeline, including row construction, optional headers, Text intercalation, and Data.Text.IO.writeFile. section 8.3
