Re: Users-prolog Digest, Vol 134, Issue 1
Duncan Patton a Campbell <[email protected]>
| Newsgroups | gmane.comp.gnu.prolog.general |
|---|---|
| Message-ID | <[email protected]> |
On Tue, 14 Oct 2014 12:00:52 -0400 [email protected] wrote: > Send Users-prolog mailing list submissions to > [email protected] > > 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 > [email protected] > > You can reach the person managing the list at > [email protected] > > 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 <[email protected]> > To: "[email protected]" <[email protected]> > Subject: Style guide / optimisation... > Message-ID: > <CAEiEuULukT24zZKAzmK_8EPEGChH01kyMbNP8+NCVuygPEHdgQ@mail.gmail.com> > 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 > [email protected] > https://lists.gnu.org/mailman/listinfo/users-prolog > > > End of Users-prolog Digest, Vol 134, Issue 1 > ******************************************** > -- Ne obliviscaris, vix ea nostra voco.