[m-dev.] for review: stream I/O
Peter Ross
petdr at miscrit.be
Mon Oct 2 18:50:41 AEDT 2000
On Mon, Oct 02, 2000 at 04:40:12PM +1100, Simon Taylor wrote:
>
> Peter Ross wrote:
> > Estimated hours taken: 40
> >
> > Implement generic stream based I/O.
>
> > string.m:
> > Define an instance of the stream typeclass which works on strings.
>
> > Index: string.m
> > ===================================================================
>
> > @@ -362,6 +369,33 @@
> >
> > :- implementation.
> > :- import_module bool, std_util, int, float, require.
> > +
> > +:- instance stream(string) where [].
> > +:- instance stream__input(string) where [
> > + pred(read_character/3) is read_char
> > +].
> > +:- instance stream__output(string) where [
> > + pred(write_character/3) is write_char
> > +].
> > +:- instance stream__duplex(string) where [].
> > +:- instance stream__side_effect_free(string) where [].
> > +
> > +:- pred read_char(stream__result(char)::out, string::di, string::uo) is det.
> > +
> > +read_char(Result, String0, String) :-
> > + ( string__first_char(String0, Char, String1) ->
> > + Result = ok(Char),
> > + copy(String1, String)
> > + ;
> > + Result = eof,
> > + copy(String0, String)
> > + ).
> > +
> > +:- pred write_char(char::in, string::di, string::uo) is det.
> > +
> > +write_char(Char, String0, String) :-
> > + string__append(String0, string__char_to_string(Char), String1),
> > + copy(String1, String).
>
> I think this is far too inefficient to be useful.
> If you still want to commit this, it would be better to add an
> abstract type `:- type string__stream ---> stream(string).',
> and make that an instance of the stream typeclasses instead.
> That would make it easier to change the implementation to
> something more efficient later.
>
Yes I realise that this is highly inefficient, but the nice thing about
it is that you can change the implementation to be whatever you want.
There definately should be a smarter string implementation which uses a
buffer which grows as necessary to do it's I/O in.
Pete
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to: mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions: mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------
More information about the developers
mailing list