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
Users-prolog@gnu.org
https://lists.gnu.org/mailman/listinfo/users-prolog

Reply via email to