[m-users.] A neater way to call chained init functions?
Julian Fondren
jfondren at minimaltype.com
Fri Jul 26 06:13:16 AEST 2019
On 2019-07-25 13:48, Julian Fondren wrote:
> setup_sdl(Res1, !IO),
> setup_sdlmix(Res2, !IO),
> setup_sdlimg(Res3, !IO),
> ( if all [X] (list.member(X, [Res1, Res2, Res3) => X = yes) then
> io.write_string("sdl initialized\n", !IO)
> else
> io.write_string("somethign failed\n", !IO)
> ).
>
> That's the nondet list.member/2 btw.
Not a good use of it though, vs.:
not list.member(no, [A, B, C])
or
not (A = no; B = no; C = no)
or
A = yes, B = yes, C = yes
A solution more like the monad one you want is to thread
initialization state through the initializers. This could involve a
lot of states, and terminate in a state telling you exactly what
failed, or on the simpler end it could just be a yes/no bool
stating whether initialization should continue, or an int that
gets incremented by each successful initialization:
some [!N] (
setup_sdl(0, !:N, !IO),
setup_sdlmix(!N, !IO),
setup_sdlimg(!N, !IO),
( if !.N = 3 then ...
More information about the users
mailing list