[Shootout-list] reversefile erlang diff

Einar Karttunen ekarttun@cs.helsinki.fi
Sun, 13 Jun 2004 20:12:38 +0300


This is a MIME-formatted message.  If you see this text it means that your
E-mail software does not support MIME-formatted messages.

--=_courier-23437-1087146759-0001-2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

Hello

Another small fix. Makes loc shorter and speeds things up.
This would be much easier if one could use files for io
instead of stdin/out.

- Einar Karttunen

--=_courier-23437-1087146759-0001-2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="reversefile.diff"

--- orig/reversefile.erl	2004-06-13 19:33:50.000000000 +0300
+++ reversefile.erl	2004-06-13 20:07:13.000000000 +0300
@@ -4,24 +4,18 @@
 
 -module(reversefile).
 -export([main/0, main/1]).
--export([init/0]).
 
 main() -> main(['1']).
 main(Args) ->
-    init(),
-    halt(0).
-
-init() ->
     Port = open_port({fd, 0, 1}, [eof, {line, 128}]),
-    printlist(rev(Port, [])).
+    read_and_print(Port),
+    halt(0).
 
-rev(Port, Lines) ->
-    receive
+read_and_print(Port) ->
+	receive 
 	{Port, {_, {_, Line}}} ->
-	    rev(Port, [Line|Lines]);
+	    read_and_print(Port),
+	    io:fwrite([Line, $\n]);
 	{Port, eof} ->
-	    Lines
+	    ok
     end.
-
-printlist([])     -> [];
-printlist([H|T])  -> io:format("~s~n", [H]), printlist(T).

--=_courier-23437-1087146759-0001-2--