users-prolog
[Top][All Lists]
Advanced

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

Style guide / optimisation...


From: emacstheviking
Subject: Style guide / optimisation...
Date: Mon, 13 Oct 2014 23:42:33 +0100

Hi,

I just knocked these out to read a file into memory for DCG practice over files. I know they might not be the best they can which is why I am asking for how to make it more efficient...

file_codes(From, Out) :-
open(From, read, S),
file_read_buf(S, [], get_code, -1, Out),
close(S).

file_chars(From, Out) :-
open(From, read, S),
file_read_buf(S, [], get_char, end_of_file, Out),
close(S).

file_read_buf(S, Acc, Fetcher, Eof, Out) :-
Reader =.. [Fetcher, S, Chr],
call(Reader),
( Chr == Eof -> reverse(Acc, Out) ; file_read_buf(S, [Chr|Acc], Fetcher, Eof, Out)).

I wanted to avoid using =.. every time I read another bit of the file; it just feels like it should be outside the loop, I tried this but it didn't do what I expected:

    file_read_buf(S, [], get_code(S, _Chr), -1, Out)

So, how can I make this more efficient and prolog-gy?

Thanks,
Sean.


reply via email to

[Prev in Thread] Current Thread [Next in Thread]