users-prolog
[Top][All Lists]
Advanced

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

Re: Users-prolog Digest, Vol 134, Issue 1


From: Duncan Patton a Campbell
Subject: Re: Users-prolog Digest, Vol 134, Issue 1
Date: Tue, 14 Oct 2014 10:48:01 -0600

On Tue, 14 Oct 2014 12:00:52 -0400
address@hidden wrote:

> Send Users-prolog mailing list submissions to
>       address@hidden
> 
> To subscribe or unsubscribe via the World Wide Web, visit
>       https://lists.gnu.org/mailman/listinfo/users-prolog
> or, via email, send a message with subject or body 'help' to
>       address@hidden
> 
> You can reach the person managing the list at
>       address@hidden
> 
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Users-prolog digest..."
> 
> 
> Today's Topics:
> 
>    1. Style guide / optimisation... (emacstheviking)
>    2. Re: Style guide / optimisation... (Daniel Diaz)
> 
> 
> ----------------------------------------------------------------------
> 
> Message: 1
> Date: Mon, 13 Oct 2014 23:42:33 +0100
> From: emacstheviking <address@hidden>
> To: "address@hidden" <address@hidden>
> Subject: Style guide / optimisation...
> Message-ID:
>       <address@hidden>
> Content-Type: text/plain; charset="utf-8"
> 
> 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?
This is what I use.  If you improve on it, let me know.

file_str(Fname,Fstr):-
        nonvar(Fstr),
        atom(Fname),!,
        file_outs(Fname,Fstr).
file_str(FnameS,Fstr):-
        nonvar(FnameS),
        nonvar(Fstr),
        atom_codes(Fname,FnameS),!,
        file_outs(Fname,Fstr).
file_str(Fname,Fstr):-
        var(Fstr),
        atom(Fname),!,
        file_ins(Fname,Fstr).
file_str(FnameS,Fstr):-
        var(Fstr),
        nonvar(FnameS),
        atom_codes(Fname,FnameS),!,
        file_ins(Fname,Fstr).

file_outs(Fname,Fstr):-
        open(Fname, 'write', Strout),
        set_stream_type(Strout,binary),!,
        writebytes(Strout,Fstr),close(Strout),!.


file_ins(Fname,Fstr):-
        open(Fname, 'read', Strin),
        set_stream_type(Strin,binary),
        readbytes(Strin,Fstr),!,close(Strin),!.

readbytes(Strin,[]):- at_end_of_stream(Strin),!.
readbytes(Strin,[Chr|ReadlS]):-
        get_byte(Strin,Chr),!,
        readbytes(Strin,ReadlS).
%readbytes(Strin,[[]|ReadlS]):-!, readbytes(Strin,ReadlS).

writebytes(_,[]):- !.
writebytes(Strout,[Chr|ReadlS]):-
        put_byte(Strout,Chr),!,
        writebytes(Strout,ReadlS).

Also

> An HTML attachment was scrubbed...
> URL: 
> <http://lists.gnu.org/archive/html/users-prolog/attachments/20141014/133cf7fd/attachment.html>

I never seem to be able to get these attachments as this
The requested URL 
/archive/html/users-prolog/attachments/20141014/133cf7fd/attachment.html was 
not found on this server.
is what lists.gnu.org always returns.

Dhu

> 
> ------------------------------
> 
> _______________________________________________
> Users-prolog mailing list
> address@hidden
> https://lists.gnu.org/mailman/listinfo/users-prolog
> 
> 
> End of Users-prolog Digest, Vol 134, Issue 1
> ********************************************
> 


-- 
Ne obliviscaris, vix ea nostra voco.



reply via email to

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