Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Message from discussion How would you replace a field in a CSV file?
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
apfel...@quantentunnel.de  
View profile  
 More options Oct 2 2006, 12:46 pm
Newsgroups: fa.haskell
From: apfel...@quantentunnel.de
Date: Mon, 02 Oct 2006 16:46:04 UTC
Local: Mon, Oct 2 2006 12:46 pm
Subject: [Haskell-cafe] Re: How would you replace a field in a CSV file?

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-C...@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.