liveBook · Manning

Data.Text.IO

As explained in Learn Haskell by Example

Data.Text.IO is the Haskell library module providing text-oriented file input and output operations.

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

How Learn Haskell by Example covers it

How deep it goes
Explains readFile and writeFile through a complete CSV-writing pipeline.
Examples
Uses a CSV-to-file writer that intercalates fields and lines before calling writeFile.
What it assumes
Assumes a Csv representation, Text values, and list transformations such as transpose and intercalate.
What you can do after
Build CSV file output with Data.Text.IO.writeFile after converting rows and fields to Text.
How it connects
Connects Text file I/O with CSV, transpose, intercalate, and function composition.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.