Browse files

Add mapAccumCSVFile

  • Loading branch information...
1 parent 204865b commit 38427dc72550d443a9c8f65f99eb7acabdff6dd5 @ozataman committed Jul 19, 2011
Showing with 30 additions and 4 deletions.
  1. +1 −0 .ghci
  2. +4 −4 csv-enumerator.cabal
  3. +25 −0 src/Data/CSV/Enumerator.hs
View
1 .ghci
@@ -0,0 +1 @@
+:set -isrc
View
8 csv-enumerator.cabal
@@ -1,5 +1,5 @@
Name: csv-enumerator
-Version: 0.9.1
+Version: 0.9.2
Synopsis: A flexible, fast, enumerator-based CSV parser library for Haskell.
Homepage: http://github.com/ozataman/csv-enumerator
License: BSD3
@@ -60,13 +60,13 @@ Library
Other-modules:
Data.CSV.Enumerator.Types
build-depends:
- attoparsec >= 0.8 && < 0.10
- , attoparsec-enumerator >= 0.2.0.3
+ attoparsec >= 0.8
+ , attoparsec-enumerator >= 0.2
, base >= 4 && < 5
, containers >= 0.3
, directory
, bytestring
- , enumerator >= 0.4.5 && < 0.5
+ , enumerator >= 0.4.5
, transformers >= 0.2
, safe
, unix-compat >= 0.2.1.1
View
25 src/Data/CSV/Enumerator.hs
@@ -32,6 +32,7 @@ module Data.CSV.Enumerator
-- * Mapping Over CSV Files
, mapCSVFile
+ , mapAccumCSVFile
, mapIntoHandle
-- * Primitive Iteratees
@@ -307,6 +308,30 @@ mapCSVFile fi s f fo = do
------------------------------------------------------------------------------
+-- | Map-accumulate over a CSV file. Similar to 'mapAccumL' in 'Data.List'.
+mapAccumCSVFile
+ :: (CSVeable r)
+ => FilePath
+ -> CSVSettings
+ -> (acc -> r -> (acc, [r]))
+ -> acc
+ -> FilePath
+ -> IO (Either SomeException acc)
+mapAccumCSVFile fi s f acc fo = do
+ res <- foldCSVFile fi s iter (acc, (Nothing, 0))
+ return $ fst `fmap` res
+ where
+ iter (a, outa) (ParsedRow (Just !r)) = foldM chain (a', outa) rs
+ where (a', rs) = f a r
+ iter (a, outa) x = do
+ outa' <- fileSink s fo outa x
+ return $ (a, outa')
+ chain (a, outa) !r = do
+ outa' <- fileSink s fo outa (ParsedRow (Just r))
+ return $ (a, outa')
+
+
+------------------------------------------------------------------------------
readCSVFile :: (CSVeable r) => CSVSettings -- ^ CSV settings
-> FilePath -- ^ FilePath
-> IO (Either SomeException [r]) -- ^ Collected data

0 comments on commit 38427dc

Please sign in to comment.