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 8117637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T04:14:14+00:00 2026-06-06T04:14:14+00:00

I have the next monad transformer: newtype Pdf’ m a = Pdf’ { unPdf’

  • 0

I have the next monad transformer:

newtype Pdf' m a = Pdf' {
  unPdf' :: StateT St (Iteratee ByteString m) a
  }
type Pdf m = ErrorT String (Pdf' m)

Basically, it uses underlying Iteratee that reads and processes pdf document (requires random-access source, so that it will not keep the document in memory all the time).

I need to implement a function that will save pdf document, and I want it to be lazy, it should be possible to save document in constant memory.

I can produce lazy ByteString:

import Data.ByteString.Lazy (ByteString)
import qualified Data.ByteString.Lazy as BS
save :: Monad m => Pdf m ByteString
save = do
  -- actually it is a loop
  str1 <- serializeTheFirstObject
  storeOffsetForTheFirstObject (BS.length str1)
  str2 <- serializeTheSecondObject
  storeOffsetForTheSecondObject (BS.length str2)
  ...
  strn <- serializeTheNthObject
  storeOffsetForTheNthObject (BS.length strn)
  table <- dumpRefTable
  return mconcat [str1, str2, ..., strn] `mappend` table

But actual output can depend on previous output. (Details: pdf document contains so called “reference table” with absolute offset in bytes of every object inside the document. It definitely depends on length of ByteString pdf object is serialized to.)

How to ensure that save function will not force entire ByteString before returning it to caller?

Is it better to take callback as an argument and call it every time I have something to output?

import Data.ByteString (ByteString)
save :: Monad m => (ByteString -> Pdf m ()) -> Pdf m ()

Is there better solution?

  • 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-06-06T04:14:15+00:00Added an answer on June 6, 2026 at 4:14 am

    The solution I found so far is Coroutine
    Example:

    proc :: Int -> Coroutine (Yield String) IO ()
    proc 0 = return ()
    proc i = do
      suspend $ Yield "Hello World\n" (proc $ i - 1)
    
    main :: IO ()
    main = do
      go (proc 10)
      where
      go cr = do
        r <- resume cr
        case r of
          Right () -> return ()
          Left (Yield str cont) -> do
            putStr str
            go cont
    

    It does the same work as callback, but caller has full control on output generation.

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

Sidebar

Related Questions

Consider the next example. I have a monad MyM that is just a StateT
I have next code: public static void createTokens(){ String test = test is a
I have next code: type TRecord1 = record myarr: array [0..31] of single: end;
I have next HTML: <input type=text id=search name=searchword value= /> <ul id=search_list> <li>first</li> <li>second</li>
I have next code: static void Main(string[] args) { byte currency; decimal amount; if
I have next arrays NAMES1: array[0..1] of string = ('NO1','NAME1'); NAMES2: array[0..1] of string
I have next date string: Thu Nov 14 0002 01:01:00 GMT+0200 (GTB Standard Time)
Map type from STL have next type: std::map< Key, Data, Compare, Alloc > As
I have next interface public interface IMyInterface { string this[string key] { get; set;
I have next code in my Entity class class Role { private List<String> tubes;

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.