intercalate and (byte)strings

I encountered the following code : -- B == Data.ByteString ; L == Data.ByteString.Lazy contents' = B.intercalate B.empty $ L.toChunks contents with a previously unencountered function intercalate. A quick google query later i knew that it's just intersperse & concat nicely bundled and started wondering why anybody would do this, as simple contents' = B.concat $ L.toChunks contents would do (probably nearly) the same. The only thing I am able to come up with is that it somehow helps streamline the memory usage (if it has some meaning). Is there some reason to use intercalate <empty> <list> instead of concat <list> (probably when dealing with non-lazy bytestrings) ? Thx, wman.

Hi wman,
-- B == Data.ByteString ; L == Data.ByteString.Lazy contents' = B.intercalate B.empty $ L.toChunks contents
with a previously unencountered function intercalate. A quick google query later i knew that it's just intersperse & concat nicely bundled and started wondering why anybody would do this, as simple
contents' = B.concat $ L.toChunks contents
would do (probably nearly) the same. The only thing I am able to come up with is that it somehow helps streamline the memory usage (if it has some meaning).
Is there some reason to use intercalate <empty> <list> instead of concat <list> (probably when dealing with non-lazy bytestrings) ?
If they do the same thing, no - and I'm pretty sure they are identical. If a much more efficient version of concat was intercalate empty, then the bytestring authors would have just defined: concat = interacalate empty It's best to use the clearest code, always. Performance is something for library authors. (It is sometimes acceptable to use the non clearest code for performance, but it is 100% mandatory to add a comment to that effect!) Thanks Neil

On Tue, 2008-12-23 at 05:21 +0100, wman wrote:
I encountered the following code :
-- B == Data.ByteString ; L == Data.ByteString.Lazy contents' = B.intercalate B.empty $ L.toChunks contents
with a previously unencountered function intercalate. A quick google query later i knew that it's just intersperse & concat nicely bundled and started wondering why anybody would do this, as simple
contents' = B.concat $ L.toChunks contents
would do (probably nearly) the same. The only thing I am able to come up with is that it somehow helps streamline the memory usage (if it has some meaning).
Is there some reason to use intercalate <empty> <list> instead of concat <list> (probably when dealing with non-lazy bytestrings) ?
I cannot see any advantage. I would be extremely surprised if the more obscure version was faster. Duncan

Thank you, guys, i somehow got the impression that there has to be some
meaning to this. It seemed unprobable, but why would anybody write it like
that if there weren't some reason to it ? ;-)))
Have a nice holidays, btw.
Cheers, wman.
On Tue, Dec 23, 2008 at 3:21 PM, Duncan Coutts
On Tue, 2008-12-23 at 05:21 +0100, wman wrote:
I encountered the following code :
-- B == Data.ByteString ; L == Data.ByteString.Lazy contents' = B.intercalate B.empty $ L.toChunks contents
with a previously unencountered function intercalate. A quick google query later i knew that it's just intersperse & concat nicely bundled and started wondering why anybody would do this, as simple
contents' = B.concat $ L.toChunks contents
would do (probably nearly) the same. The only thing I am able to come up with is that it somehow helps streamline the memory usage (if it has some meaning).
Is there some reason to use intercalate <empty> <list> instead of concat <list> (probably when dealing with non-lazy bytestrings) ?
I cannot see any advantage. I would be extremely surprised if the more obscure version was faster.
Duncan

On Tue, Dec 23, 2008 at 04:06:38PM +0100, wman wrote:
Thank you, guys, i somehow got the impression that there has to be some meaning to this. It seemed unprobable, but why would anybody write it like that if there weren't some reason to it ? ;-)))
My guess is that it was probably to gain compatability between different ByteString versions. In older versions, the lazy byte string was just a newtype around a list of bytestrings so you didn't have 'toChunks'. A hack to get around another hack (CPP). John -- John Meacham - ⑆repetae.net⑆john⑈
participants (4)
-
Duncan Coutts
-
John Meacham
-
Neil Mitchell
-
wman