lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jul 10, 2017, at 5:08 PM, Gavin Holt <holtgavin@gmail.com> wrote:

> I would be grateful for some advice about how to manage the ORS. You
> will see that for the last source line I avoid appending the ORS. However,
> if the last line doesn't generate any output I can't get back the previously
> output ORS!

Here’s a closure version? It seemed easier than the object version.

function ors_stream(ORS, output)
    local needs_ors = false;
    return function(s)
	if needs_ors then output(ORS) end
	needs_ors=true
	output(s)
    end
end

my_stream = ors_stream("\n----\n", io.write)
my_stream("hello, world")
my_stream("hello, again")

Jay