cassava-streams-0.3.0.5: io-streams interface for the cassava CSV library.
Safe HaskellNone
LanguageHaskell2010

System.IO.Streams.Csv.Tutorial

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

Instances

Instances details
Eq Item Source # 
Instance details

Defined in System.IO.Streams.Csv.Tutorial

Methods

(==) :: Item -> Item -> Bool #

(/=) :: Item -> Item -> Bool #

Show Item Source # 
Instance details

Defined in System.IO.Streams.Csv.Tutorial

Methods

showsPrec :: Int -> Item -> ShowS #

show :: Item -> String #

showList :: [Item] -> ShowS #

FromNamedRecord Item Source # 
Instance details

Defined in System.IO.Streams.Csv.Tutorial

ToNamedRecord Item Source # 
Instance details

Defined in System.IO.Streams.Csv.Tutorial

data TState Source #

Possible states for a to-do item.

Constructors

Todo

Item needs to be completed.

Done

Item has been finished.

Instances

Instances details
Eq TState Source # 
Instance details

Defined in System.IO.Streams.Csv.Tutorial

Methods

(==) :: TState -> TState -> Bool #

(/=) :: TState -> TState -> Bool #

Show TState Source # 
Instance details

Defined in System.IO.Streams.Csv.Tutorial

FromField TState Source # 
Instance details

Defined in System.IO.Streams.Csv.Tutorial

ToField TState Source # 
Instance details

Defined in System.IO.Streams.Csv.Tutorial

Methods

toField :: TState -> Field #

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.