[m-users.] Useful predicates for loops of IO actions

Volker Wysk post at volker-wysk.de
Wed Dec 22 02:18:48 AEDT 2021


Hi Mercury developers!

I'm using four meta-predicates which deal with loops and IO, and I find them
useful. Maybe you want to include them in the Mercury libraries? They
correspond to map, foldl and filter, but they additonally thread through an
IO state. They are the same as Haskell's "mapM", "mapM_", "foldM" and
"filterM" combinators.

Here are the IO-versions for map. In the first one, the IO-predicate's
result is collected and returned as a list. In the second one, the IO-
predicate doesn't have a return value. It's just the threading of the IO
state that's is wanted:

:- pred map_io(pred(T, U, io, io)::in(pred(in, out, di, uo) is det),
               list(T)::in,
               list(U)::out,
               io::di, io::uo)
   is det.

:- pred map_io(pred(T, io, io)::in(pred(in, di, uo) is det),
               list(T)::in,
               io::di, io::uo)
   is det.

This is the IO-version of foldl:

:- pred fold_io(pred(T, A, A, io, io)::in(pred(in, in, out, di, uo) is det),
                list(T)::in,
                A::in,
                A::out,
                io::di, io::uo)
   is det.

And the IO version of list.filter/4:

:- pred filter_io(
    pred(T, bool, io, io)::in(pred(in, out, di, uo) is det),
    list(T)::in,
    list(T)::out,
    list(T)::out,
    io::di, io::uo)
   is det.

If you'd like to include these predicates, I'd provide proper comments and,
of course, the implementation.

Happy hacking, Volker
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part
URL: <http://lists.mercurylang.org/archives/users/attachments/20211221/5fc5c861/attachment.sig>


More information about the users mailing list