users-prolog
[Top][All Lists]
Advanced

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

Re: Style guide / optimisation...


From: Daniel Diaz
Subject: Re: Style guide / optimisation...
Date: Tue, 14 Oct 2014 08:57:21 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.1.2

Hi

You are right =.. is not very efficient. You can use call/2-N to avoid this
http://www.gprolog.org/manual/html_node/gprolog042.html#sec194

Also the reverse is time and space consuming. This is strange because you use an accumulator: this is generally used to avoid append or reverse. In fact the accumulator is not needed and avoids the use of reverse.

Here is the resulting version:

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, Fetcher, Eof, Out) :-
    call(Fetcher, S, Chr),
    (   Chr \== Eof ->
        Out = [Chr|Out1],
        file_read_buf(S, Fetcher, Eof, Out1)
    ;
        Out = []
    ).



Daniel

Le 14/10/2014 00:42, emacstheviking a écrit :
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?



--
Ce message a été vérifié par MailScanner pour des virus ou des polluriels et rien de suspect n'a été trouvé.

reply via email to

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