Handling multiple files at the same time

I think you should be able to nest open calls.

open("in.txt","r") do io_in
  open("out.txt","r") do io_out
   readline(io_in) |> print
   readline(io_out) |> print
  end
end

you can also use open without a do block.

io_in = open("in.txt","r")
io_out = open("in.txt","w")
# do stuff
close(io_in)
close(io_out)

The implementation when using a function as a first argument or a do block looks like this:

function open(f::Function, args...; kwargs...)
    io = open(args...; kwargs...)
    try
        f(io)
    finally
        close(io)
    end
end

However, I am not sure if there is an implementation of replace that acts on data streams.

1 Like