Stream merge: Difference between revisions

Added PicoLisp
m (→‎version 2: changed the wording of the comment in the output section, simplified the RDR subroutine.)
(Added PicoLisp)
Line 364:
 
say merge_streams([ @*ARGS».&open ]);</lang>
 
=={{header|PicoLisp}}==
<lang PicoLisp>(de streamMerge @
(let Heap
(make
(while (args)
(let? Fd (next)
(if (in Fd (read))
(link (cons @ Fd))
(close Fd) ) ) ) )
(make
(while Heap
(link (caar (setq Heap (sort Heap))))
(if (in (cdar Heap) (read))
(set (car Heap) @)
(close (cdr (pop 'Heap))) ) ) ) ) )</lang>
<pre>$ cat a
3 14 15
 
$ cat b
2 17 18
 
$ cat c
 
$ cat d
2 3 5 7</pre>
Test:
<lang PicoLisp>(test (2 3 14 15 17 18)
(streamMerge
(open "a")
(open "b") ) )
 
(test (2 2 3 3 5 7 14 15 17 18)
(streamMerge
(open "a")
(open "b")
(open "c")
(open "d") ) )</lang>
'streamMerge' works with non-numeric data as well, and also - instead of calling
'open' on a file or named pipe - with the results of 'connect' or 'listen' (i.e.
sockets).
 
== {{header|Python}} ==
Anonymous user