[Haskell-cafe] Re: optimization help

Bulat Ziganshin bulat.ziganshin at gmail.com
Sat Oct 14 04:43:48 EDT 2006

Hello apfelmus,

Thursday, October 12, 2006, 4:42:14 PM, you wrote:

> A better solution would be to begin output before the the whole input is
> read, thus making things more lazy. This can be done the following way:
> from the input, construct a lazy list of (date,line) pairs. Then, let
> foldM thread a map from dates to corresponding output file pointers
> through the list and, at the same time, use the file pointers to output
> the line in question via appendFile. This way, every line consumed is
> immediately dispatched to its corresponding output file and things
> should only require memory for the different dates, besides buffering.

> In a setting without IO, the task corresponds to the "Optimization
> Problem" discussed at length in September on this list. The problem here
> is that writeFile currently cannot be interleaved lazily, this has to be
> simulated with appendFile. We can read files lazily but we cannot output
> them lazily.
> Can this be remedied? Can there be a version of writeFile which is, in a
> sense, dual to getContents?

this can be solved in other way. here is a program that reads stdin
and puts to stdout lines starting with '>' and to stderr the rest.
note that our main processing function is pure:

main = do a <- getContents
          let b = map (processing stdout stderr) (lines a)
          mapM_ (\(file,line) -> hPutStrLn file line) b

processing file1 file2 line
  = if ">" `isPrefixOf` line
      then (file1,line)
      else (file2,line)

Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com

More information about the Haskell-Cafe mailing list