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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:01:00+00:00 2026-06-13T02:01:00+00:00

I have function a f which returns a large Integer. Nearing end the program

  • 0

I have function a f which returns a large Integer. Nearing end the program has to add up all the returned values of f. The physical memory of this computer is too limited to store all the returned values of f. So I would need to put this into a file buffer. Would TVars be able to handle Integers? Is there a solution in which I can throw all the returned values of f? Also, could the separate threads be able to read it and buffer it at the the same time?

  • 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-13T02:01:01+00:00Added an answer on June 13, 2026 at 2:01 am

    Your question isn’t all that clear. From what I understand you need to store all the results that a function f has returned during the run of a program and since there are large number of such results you would like to store those results in a file. Since it would be inefficient to actually store each result as soon as it was computed you would like to implement a form of buffering.

    If this is the case, you could use something like Chan for example which is an unbounded blocking FIFO queue. And to answer one of your concerns, this structure was specifically designed for concurrent access from multiple threads.

    So you could run your main program where you would call f, and for each call you would also insert the result into the Chan. You would also spawn another thread which will continuously read from the Chan and write the results in a file.

    Now, if the rate at which the main thread (the one that calls f) is much higher than the rate at which the other thread stores the results on disk then you are back to your original problem in which results pile up in the Chan and you remain out of memory at some point. For this particular case you could use something like BoundedChan which is similar to Chan but will block at insertion when the channel is full. In this scenario, the main thread may have to wait sometimes for the writing thread to store the results on disk, but you have the guarantee that you’ll never fill-up the memory with the many results of f.

    We can actually build a nice abstraction for this. We can imagine a functional traceable that given a function f and a way to store values gives us a function which returns the same results as f but as a side effect it also stores the results for later analysis.

    traceable :: (a -> b) -> (b -> IO ()) -> (a -> IO b)
    traceable f store = \x -> do
        let result = f x
        store result
        return result
    

    In your case, the program could look something like this:

    f :: Int -> Int
    f = ... -- implementation of f here
    
    main = do
        ch <- newChan
        traceableF = traceable f (writeChan ch)
        forkIO $ resultWriter ch
        -- the main program which calls traceableF here ...
    
    resultWriter :: Chan Int -> IO ()
    resultWriter ch = do
        f <- obtainFileHandler
        forever $ do
            result <- readChan
            writeToFile f result
    

    You may also need to write some logic so that the main thread waits for the resultWriter thread to finish writing to disk, but basically that’s about it.

    Hope this answers your question.

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

Sidebar

Related Questions

I have a function which returns a one-sided intersection of values between two input
If you have a C function which returns an integer, you could write a
I have a native/unmanaged DLL and it has a CreateObject function which returns a
I am having a strange problem. I have a function which returns a large
I have a function which returns 3 numbers, e.g.: def numbers(): return 1,2,3 usually
I have this function which returns a datatype InetAddress[] public InetAddress [] lookupAllHostAddr(String host)
If I have a function which returns a reference cursor for a query, how
Say, i have a function which returns a reference and i want to make
I have a php function which returns JSON in response to a ajax request
i have a php function which returns a random json encoded color <?php function

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.