Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p>If this is the case, you could use something like <a href="http://www.haskell.org/ghc/docs/7.4.2/html/libraries/base/Control-Concurrent-Chan.html" rel="nofollow">Chan</a> 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.</p> <p>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.</p> <p>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 <a href="http://hackage.haskell.org/packages/archive/BoundedChan/latest/doc/html/Control-Concurrent-BoundedChan.html" rel="nofollow">BoundedChan</a> 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.</p> <p>We can actually build a nice abstraction for this. We can imagine a functional <code>traceable</code> 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.</p> <pre><code>traceable :: (a -&gt; b) -&gt; (b -&gt; IO ()) -&gt; (a -&gt; IO b) traceable f store = \x -&gt; do let result = f x store result return result </code></pre> <p>In your case, the program could look something like this:</p> <pre><code>f :: Int -&gt; Int f = ... -- implementation of f here main = do ch &lt;- newChan traceableF = traceable f (writeChan ch) forkIO $ resultWriter ch -- the main program which calls traceableF here ... resultWriter :: Chan Int -&gt; IO () resultWriter ch = do f &lt;- obtainFileHandler forever $ do result &lt;- readChan writeToFile f result </code></pre> <p>You may also need to write some logic so that the main thread waits for the <code>resultWriter</code> thread to finish writing to disk, but basically that's about it.</p> <p>Hope this answers your question.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload