Safe Haskell | None |
---|---|
Language | Haskell2010 |
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.
Types representing to-do items and their state
A to-do item.
Possible states for a to-do item.
Functions which use cassava-streams functions
:: 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 Item
s 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 Item
s 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.
:: String | Items with this title are marked as |
-> 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.