Stream merge: Difference between revisions

Line 1,237:
<pre>1245781011
123456789101112</pre>
 
=={{header|Julia}}==
{{trans|C}}
The IOStream type in Julia encompasses any data stream, including file I/O and TCP/IP. The IOBuffer used here maps a stream to a buffer in memory, and so allows an easy simulation of two streams without opening files.
<lang Julia>
function merge(stream1, stream2, T=Char)
if !eof(stream1) && !eof(stream2)
b1 = read(stream1, T)
b2 = read(stream2, T)
while !eof(stream1) && !eof(stream2)
if b1 <= b2
print(b1)
if !eof(stream1)
b1 = read(stream1, T)
end
else
print(b2)
if !eof(stream2)
b2 = read(stream2, T)
end
end
end
while !eof(stream1)
print(b1)
b1 = read(stream1, T)
end
print(Char(b1[1]))
while !eof(stream2)
print(b2)
b2 = read(stream2, T)
end
print(b2)
end
end
 
const halpha1 = "acegikmoqsuwy"
const halpha2 = "bdfhjlnprtvxz"
const buf1 = IOBuffer(halpha1)
const buf2 = IOBuffer(halpha2)
 
merge(buf1, buf2, Char)
println("\nDone.")
 
<lang>{{output}}<pre>
abcdefghijklmnopqrstuvwyxz
Done.
</pre>
 
=={{header|Kotlin}}==
4,107

edits