[m-users.] A neater way to call chained init functions?

Julian Fondren jfondren at minimaltype.com
Fri Jul 26 10:09:27 AEST 2019


On 2019-07-25 11:43, emacstheviking wrote:
> is there a more idiomatic / efficient / less $%^&*( ugly way to do
> this please...

I'd like to point out that I have no idea as far as idiomatic
and efficient goes.

Especially so with this option:

   :- module initall.
   :- interface.
   :- import_module io.
   :- pred main(io::di, io::uo) is det.
   :- implementation.
   :- import_module list, bool.

   :- type initializer == (pred(bool, io, io)).
   :- inst initializer == (pred(out, di, uo) is det).

   :- func initializer >> initializer = initializer.
   :- mode in(initializer) >> in(initializer) = out(initializer).
   A >> B = C :-
       C = (pred(R::out, !.IO::di, !:IO::uo) is det :-
           A(R1, !IO),
           ( R1 = yes, B(R, !IO) ; R1 = no, R = no)).

   :- pred setup_sdl(bool::out, io::di, io::uo) is det.
   :- pred setup_sdlmix(bool::out, io::di, io::uo) is det.
   :- pred setup_sdlimg(bool::out, io::di, io::uo) is det.
   :- pred setup_sdlmaybe(bool::in, bool::out, io::di, io::uo) is det.

   setup_sdl(yes, !IO) :- io.write_string("at sdl\n", !IO).
   setup_sdlmix(yes, !IO) :- io.write_string("at sdlmix\n", !IO).
   setup_sdlimg(yes, !IO) :- io.write_string("at sdlimg\n", !IO).
   setup_sdlmaybe(B, B, !IO) :-
       io.write_string("at sdlmaybe: ", !IO),
       io.write_line(B, !IO).

   main(!IO) :-
       Init = setup_sdl
           >> setup_sdlmaybe(no)
           >> setup_sdlmix
           >> setup_sdlimg,
       Init(Res, !IO),
       io.write_line(Res, !IO).


More information about the users mailing list