Style guide / optimisation...

emacstheviking <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <CAEiEuULukT24zZKAzmK_8EPEGChH01kyMbNP8+NCVuygPEHdgQ@mail.gmail.com>
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.

_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.