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

Sean Charles (emacstheviking) objitsu at gmail.com
Wed Dec 22 03:28:24 AEDT 2021


Hi Volker, I use code like this all the time for !IO and folding,

run_info(Options, !IO) :-
    aio.format("@cy:Environment variables:@-\n", [], !IO),

    list.foldl(
        (pred(S::in, !.IO::di, !:IO::uo) is det :-
            get_env_str(S, "", ENV, !IO),
            aio.format("@yl:%15s:@- %s\n",
                [s(S), s(ENV)],
                !IO)
        ), ["FELT_INST", "FELT_DOCS", "FELT_PAGER", "FELT_EDITOR",
            "FELT_LEX",  "FELT_AST",  "FELT_TRN"],
        !IO),

    aio.format("@cy:Command line options:@-\n", [], !IO),

    list.foldl(
        (pred(K::in, !.IO::di, !:IO::uo) is det :-
            map.lookup(Options, K, V),
            aio.format("@yl:%15s:@- %s\n",
                [s(string(K)), s(string(V))],
                !IO)
        ),
        map.sorted_keys(Options),
        !IO).

Like Zoltan says, the code practically writes itself, I’ve not found the need to for threading !IO through anything to be honest!

All the best,
Sean





> On 21 Dec 2021, at 16:21, Volker Wysk <post at volker-wysk.de> wrote:
> 
> Am Mittwoch, dem 22.12.2021 um 02:51 +1100 schrieb Zoltan Somogyi:
>> 
>> On Tue, 21 Dec 2021 16:18:48 +0100, Volker Wysk <post at volker-wysk.de> wrote:
>>> 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?
>> 
>> Three out of the four are already there, in more general forms. Instead of
>> the last pair of arguments required to be I/O states, they can be any type,
>> and their modes do not have to be di/uo; they can be e.g. in/out.
> 
> Now I'm feeling bashful... :) In the future, I'll make it a habit to read
> the documentation more thoroughly. It's, in part, because I didn't fully
> understand what was meant by "accumulator". (I have a different
> understanding of it).
> 
> Bye, Volker
> 
> _______________________________________________
> users mailing list
> users at lists.mercurylang.org
> https://lists.mercurylang.org/listinfo/users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mercurylang.org/archives/users/attachments/20211221/43fbd43a/attachment.html>


More information about the users mailing list