Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6971319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:49:28+00:00 2026-05-27T16:49:28+00:00

I have a function that takes a lazy ByteString , that I wish to

  • 0

I have a function that takes a lazy ByteString, that I wish to have return lists of strict ByteStrings (the laziness should be transferred to the list type of the output).

import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
csVals :: L.ByteString -> [B.ByteString]

I want to do this for various reasons, several lexing functions require strict ByteStrings, and I can guarantee the outputted strict ByteStrings in the output of csVals above are very small.

How do I go about “strictifying” ByteStrings without chunking them?

Update0

I want to take a Lazy ByteString, and make one strict ByteString containing all its data.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:49:29+00:00Added an answer on May 27, 2026 at 4:49 pm

    Like @sclv said in the comments above, a lazy bytestring is just a list of strict bytestrings. There are two approaches to converting lazy ByteString to strict (source: haskell mailing list discussion about adding toStrict function) – relevant code from the email thread below:

    First, relevant libraries:

    import qualified Data.ByteString               as B
    import qualified Data.ByteString.Internal      as BI
    import qualified Data.ByteString.Lazy          as BL
    import qualified Data.ByteString.Lazy.Internal as BLI
    import           Foreign.ForeignPtr
    import           Foreign.Ptr
    

    Approach 1 (same as @sclv):

    toStrict1 :: BL.ByteString -> B.ByteString
    toStrict1 = B.concat . BL.toChunks
    

    Approach 2:

    toStrict2 :: BL.ByteString -> B.ByteString
    toStrict2 BLI.Empty = B.empty
    toStrict2 (BLI.Chunk c BLI.Empty) = c
    toStrict2 lb = BI.unsafeCreate len $ go lb
      where
        len = BLI.foldlChunks (\l sb -> l + B.length sb) 0 lb
    
        go  BLI.Empty                   _   = return ()
        go (BLI.Chunk (BI.PS fp s l) r) ptr =
            withForeignPtr fp $ \p -> do
                BI.memcpy ptr (p `plusPtr` s) (fromIntegral l)
                go r (ptr `plusPtr` l)
    

    If performance is a concern, I recommend checking out the email thread above. It has criterion benchmark as well. toStrict2 is faster than toStrict1 in those benchmarks.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my programm I have function that takes the image from camera and should
I have a function that takes 2 parameters and return a table. Is it
I have a function that takes a list of floats and returns the same
I have a function that takes, amongst others, a parameter declared as int privateCount
I have a function that takes a struct, and I'm trying to store its
I have a function that takes pointer to pointer to struct. struct ABC; void
I have a function that takes a string-like argument. I want to decide if
I have a function that takes a const D3DVECTOR3 *pos , but I have
I have a function that takes 2 parameters : 1 = XML file, 2
I have a function that takes variadic arguments, that I obtain from func_get_args() .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.