cassava-streams-0.3.0.2: io-streams interface for the cassava CSV library.

Safe HaskellNone
LanguageHaskell2010

System.IO.Streams.Csv.Tutorial

Contents

Description

A simple tutorial on using the cassava-streams library to glue together cassava and io-streams.

Note: if you're reading this on Hackage or in Haddock then you should switch to source view with the "Source" link at the top of this page or open this file in your favorite text editor.

Synopsis

Types representing to-do items and their state

data Item Source #

A to-do item.

Constructors

Item 

Fields

data TState Source #

Possible states for a to-do item.

Constructors

Todo

Item needs to be completed.

Done

Item has been finished.

Functions which use cassava-streams functions

onlyTodo Source #

Arguments

:: Handle

Input handle where CSV data can be read.

-> Handle

Output handle where CSV data can be written.

-> IO () 

The onlyTodo function reads to-do Items from the given input handle (in CSV format) and writes them back to the output handle (also in CSV format), but only if the items are in the Todo state. In another words, the CSV data is filtered so that the output handle only receives to-do Items which haven't been completed.

The io-streams handleToInputStream function is used to create an InputStream ByteString stream from the given input handle.

That stream is then given to the cassava-streams function decodeStreamByName which converts the InputStream ByteString stream into an InputStream Item stream.

Notice that the cassava-streams function onlyValidRecords is used to transform the decoding stream into one that only produces valid records. Any records which fail type conversion (via FromNamedRecord or FromRecord) will not escape from onlyValidRecords but instead will throw an exception.

Finally the io-streams filter function is used to filter the input stream so that it only produces to-do items which haven't been completed.

markDone Source #

Arguments

:: String

Items with this title are marked as Done.

-> Handle

Input handle where CSV data can be read.

-> Handle

Output handle where CSV data can be written.

-> IO () 

The markDone function will read to-do items from the given input handle and mark any matching items as Done. All to-do items are written to the given output handle.