# outputs during runtime

[email protected] [email protected]
30 Jul 2001 10:34:32 -0000

Nicole Gabler <[email protected]
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
> wrote:

> During runtime I want the program to output =
> information about the status every time another file is parsed. That =
> doesn't work, the output appears much later, mostly not until end of =
> program. But I need these outputs at that specific time, so what can I =
> do to manage it?

Perhaps the following fragment from my prelude extensions helps.
It requires import IO''.

Most probably you would either be calling

> printList "\n" statusInformationList

or factor out the flushing part
for interleaving with your other IO actions.

Wolfram

%{{{ hPutList, printList
It is frequently annoying if a long-running program
writes a list of items to a channel or file,
and while it calculates the next item,
the last item has usually not yet been (completely) written
because at least part of it is still held in some output buffer.

The following functions flush the output buffer after every
item.
The most general variant, \verb|hPutList|,
is parameterised by a separator string for list items,
a \verb|shows| function for items,
and a handle for output.
The variant \verb|putList| writes always to the standard output channel,
and \verb|printList| in addition uses the \verb|shows| function
supplied by the type class system.

\begin{code}
hPutList :: String -> (a -> ShowS) -> Handle -> [a] -> IO ()
hPutList sep shows h = mapM_ f
where f x = hPutStr h (shows x sep) >> hFlush h

putList :: String -> (a -> ShowS) -> [a] -> IO ()
putList sep shows = hPutList sep shows stdout

printList :: Show a => String -> [a] -> IO ()
printList sep = putList sep shows
\end{code}
%}}}