[Haskell-cafe] efficient chop
thomas.dubuisson at gmail.com
Wed Sep 14 05:14:46 CEST 2011
This was a recent question on StackOverflow:
Where I started:
If you have serious text processing needs then use the text package
A quick Criterion benchmark tells me that (for a particularly long
string of words with spaces and ~200 pre and post spaces) my trim
takes 1.6 ms, the trim using reverse takes 3.5ms, and Data.Text.strip
takes 0.0016 ms.
On Tue, Sep 13, 2011 at 8:03 PM, Kazu Yamamoto <kazu at iij.ad.jp> wrote:
> Hello Cafe,
> I would like to have an efficient implementation of the chop function.
> As you guess, the chop function drops spaces in the tail of a list.
> chop " foo bar baz "
> -> " foo bar baz"
> A naive implementation is as follows:
> chopReverse :: String -> String
> chopReverse = reverse . dropWhile isSpace . reverse
> But this is not elegant. foldr version is as follows:
> chopFoldr :: String -> String
> chopFoldr = foldr f 
> f c 
> | isSpace c = 
> | otherwise = c:
> f c cs = c:cs
> But this code is slower than chopReverse in some cases.
> Are there any more efficient implementations of chop? Any suggestions?
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe