[Haskell-cafe] Re: How would you replace a field in a CSV file?

2006-10-09 Thread John Goerzen
On 2006-10-01, Pete Kazmier [EMAIL PROTECTED] wrote:
 For those that know python, here is a very simple implementation that
 happens to be very fast compared to my Haskell version and very short:

 for line in sys.stdin:
 fields = line.split(',')

Of course, this doesn't handle quoted values that contain commas, which
are among the many joys* of parsing CSV files.

I might just point out the existance of MissingH.Str.CSV to you, which
can read and write CSV files.  See
http://gopher.quux.org:70/devel/missingh/html/MissingH-Str-CSV.html for
details.

A one-line call gives you a [[String]].

This uses Parsec, so it will not be a top-notch performer, but it is
elegant ;-)

* I mean joy sarcastically, in case it wasn't obvious.



___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: How would you replace a field in a CSV file?

2006-10-02 Thread apfelmus
Pete Kazmier wrote:
 import Data.ByteString.Lazy.Char8 as B hiding (map,foldr)
 import Data.List (map)
 import Data.Map as M hiding (map)
 
 -- This will be populated from a file
 dict = M.singleton (B.pack Pete) (B.pack Kazmier)
 
 main = B.interact $ B.unlines . map doline . B.lines
 where doline= B.join comma . mapIndex fixup . B.split ','
   comma = B.singleton ','
   fixup 3 s = M.findWithDefault s s dict
   fixup n s = s
 
 -- f is supplied the index of the current element being processed
 mapIndex :: (Int - ByteString - ByteString) - [ByteString] -
 [ByteString]
 mapIndex f xs = m xs 0
 where m []  _ = []
   m (x:xs') i = f i x : (m xs' $! i+1)

How about

import Data.ByteString.Lazy.Char8 as B hiding (map,foldr)
import Data.List (map)
import Data.Map as M hiding (map)

dict = M.singleton (B.pack Pete) (B.pack Kazmier)

main = B.interact $ B.unlines . map doline . B.lines
where
doline  = B.join comma . zipWith ($) fixup9 . B.split ','
fixup9  = fixup 9
fixup n = replicate n id
  ++ [\s - M.findWithDefault s s dict] ++ repeat id

Note that fixup9 is shared intentionally across different invocations of
doline. The index n starts at 0.


Also note that because (compare :: (Byte)String - ..) takes time
proportional to the string length, the use of Map will inevitably
introduce a constant factor. But I'm still happy you didn't use arrays
or hash tables (urgh!) :)

In any case, tries are *the* natural data structure for (in)finite maps
in functional languages, see also

Ralf Hinze. Generalizing generalized tries. Journal of Functional
Programming, 10(4):327-351, July 2000
http://www.informatik.uni-bonn.de/~ralf/publications/GGTries.ps.gz


Regards,
apfelmus

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Re: How would you replace a field in a CSV file?

2006-10-01 Thread Pete Kazmier
[EMAIL PROTECTED] writes:

 For such a small self-contained task, I don't think Haskell
 is any better than Python.

I figured as much, but I thought with the new FPS lazy bytestrings it
might have a chance in terms of raw speed.  On the other side of the
coin, in terms of elegance, I thought I'd ask as haskellers always
amaze me with their one-liners :-)

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe