[Haskell-cafe] efficient chop
Kazu Yamamoto ( 山本和彦 )
kazu at iij.ad.jp
Wed Sep 14 05:29:52 CEST 2011
Hello,
Of course, I use ByteString or Text for real programming. But I would
like to know whether or not there are any efficient methods to remove
a tail part of a list.
--Kazu
From: Thomas DuBuisson <thomas.dubuisson at gmail.com>
Subject: Re: [Haskell-cafe] efficient chop
> This was a recent question on StackOverflow:
>
> http://stackoverflow.com/questions/6270324/in-haskell-how-do-you-trim-whitespace-from-the-beginning-and-end-of-a-string/6270382#6270382
>
> Where I started:
>
> If you have serious text processing needs then use the text package
> from hackage.
>
> And concluded:
>
> 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.
>
> Cheers,
> Thomas
>
> 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 []
>> where
>> 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?
>>
>> --Kazu
>>
>> _______________________________________________
>> Haskell-Cafe mailing list
>> Haskell-Cafe at haskell.org
>> http://www.haskell.org/mailman/listinfo/haskell-cafe
>>
>
More information about the Haskell-Cafe
mailing list