title: Python: writing files in functional programming style
body: <p>How are we supposed to write files in Python while staying functionally pure? Normally I would do something like this </p>
  
  <pre><code>from typing import Iterable
  from io import IOBase
  
  
  def transform_input(input_lines: Iterable[str]) -&gt; Iterable[str]: ...
  
  
  def print_pack(input_lines: Iterable[str], output: IOBase) -&gt; None:
      for line in input_lines:
          print(line, file=output)
  
  
  def main(*args, **kwargs):
      # Somehow we get a bunch iterables with strings and a list of output streams
      packs_of_input = ... # Iterable[Iterable[str]]
      output_streams = ... # Iterable[IOBase]
      packs_to_print = map(transform_input, packs_of_input)
      for pack, output_stream in zip(packs_to_print, output_streams):
          print_pack(pack, output_stream)
  </code></pre>
  
  <p>We can replace the <code>for</code>-loop with something like this</p>
  
  <pre><code>list(map(lambda pack_stream: print_pack(*pack_stream), zip(packs_to_print, output_streams))
  </code></pre>
  
  <p>but it would only make it look like the printing is done functionally. The problem is that <code>print_pack</code> is not a pure function, that is all its effort results in a side-effect and it returns nothing. 
  How are we supposed to write files and remain functionally-pure (or almost pure)? </p>