How to do line oriented i/o in constant space?

Torbjorn Tornkvist tobbe@REDACTED
Mon Oct 9 12:52:51 CEST 2000


-module(pipe).
-compile(export_all).

%%---------------------------------------------------
%% # cat indata | erl -s pipe -nouser > outdata
%% 1 2 3 4 5
%% 5 4 3 2 1
%% 5 1 2 3 4
%% 4 3 2 1 5
%%---------------------------------------------------

start() ->
    spawn(pipe,init,[]).

init() ->
    Port = open_port({fd, 0, 1},[eof]),
    loop(Port).

loop(Port) ->
    receive
	{Port, {data, What}} ->
	    Port ! {self(), {command, What}},
	    loop(Port);
	{Port, eof} ->
	    halt()
    end.





More information about the erlang-questions mailing list