[m-rev.] diff: add assoc_list.foldl_{keys,values}_only/4

Julien Fischer juliensf at csse.unimelb.edu.au
Fri May 27 15:49:51 AEST 2011


Branches: main

Add predicates for folding over only the keys or values of an association list.

library/assoc_list.m:
 	Add the predicates foldl_keys_only/4 and foldl_values_only/4.

NEWS:
 	Announce the additions.

Julien.

Index: NEWS
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/NEWS,v
retrieving revision 1.578
diff -u -r1.578 NEWS
--- NEWS	26 May 2011 06:20:02 -0000	1.578
+++ NEWS	27 May 2011 05:39:05 -0000
@@ -158,6 +158,9 @@
  * A new function, array.generate/2, and new predicate, array.generate_foldl/5,
    can be used to create and fill new arrays.

+* We have added the new predicate assoc_list.foldl_keys_only/4 and
+  assoc_list.foldl_values_only/4 for folding over just keys or values
+  an association list.

  NEWS for Mercury 11.01
  ----------------------
Index: library/assoc_list.m
===================================================================
RCS file: /home/mercury/mercury1/repository/mercury/library/assoc_list.m,v
retrieving revision 1.28
diff -u -r1.28 assoc_list.m
--- library/assoc_list.m	20 May 2011 17:24:32 -0000	1.28
+++ library/assoc_list.m	27 May 2011 05:28:53 -0000
@@ -135,6 +135,50 @@
  :- func assoc_list.merge(assoc_list(K, V), assoc_list(K, V))
      = assoc_list(K, V).

+    % assoc_list.foldl_keys_only(Pred, List, Start End) calls Pred
+    % with each key in List (working left-to-right) and an accumulator
+    % (with initial value of Start), and returns the final value in End.
+    %
+:- pred assoc_list.foldl_keys_only(pred(K, A, A), assoc_list(K, V), A, A).
+:- mode assoc_list.foldl_keys_only(pred(in, in, out) is det, in,
+    in, out) is det.
+:- mode assoc_list.foldl_keys_only(pred(in, mdi, muo) is det, in,
+    mdi, muo) is det.
+:- mode assoc_list.foldl_keys_only(pred(in, di, uo) is det, in,
+    di, uo) is det.
+:- mode assoc_list.foldl_keys_only(pred(in, in, out) is semidet, in,
+    in, out) is semidet.
+:- mode assoc_list.foldl_keys_only(pred(in, mdi, muo) is semidet, in,
+    mdi, muo) is semidet.
+:- mode assoc_list.foldl_keys_only(pred(in, di, uo) is semidet, in,
+    di, uo) is semidet.
+:- mode assoc_list.foldl_keys_only(pred(in, in, out) is multi, in,
+    in, out) is multi.
+:- mode assoc_list.foldl_keys_only(pred(in, in, out) is nondet, in,
+    in, out) is nondet.
+
+    % assoc_list.foldl_values_only(Pred, List, Start End) calls Pred
+    % with each value in List (working left-to-right) and an accumulator
+    % (with initial value of Start), and returns the final value in End.
+    %
+:- pred assoc_list.foldl_values_only(pred(V, A, A), assoc_list(K, V), A, A).
+:- mode assoc_list.foldl_values_only(pred(in, in, out) is det, in,
+    in, out) is det.
+:- mode assoc_list.foldl_values_only(pred(in, mdi, muo) is det, in,
+    mdi, muo) is det.
+:- mode assoc_list.foldl_values_only(pred(in, di, uo) is det, in,
+    di, uo) is det.
+:- mode assoc_list.foldl_values_only(pred(in, in, out) is semidet, in,
+    in, out) is semidet.
+:- mode assoc_list.foldl_values_only(pred(in, mdi, muo) is semidet, in,
+    mdi, muo) is semidet.
+:- mode assoc_list.foldl_values_only(pred(in, di, uo) is semidet, in,
+    di, uo) is semidet.
+:- mode assoc_list.foldl_values_only(pred(in, in, out) is multi, in,
+    in, out) is multi.
+:- mode assoc_list.foldl_values_only(pred(in, in, out) is nondet, in,
+    in, out) is nondet.
+
  %-----------------------------------------------------------------------------%
  %-----------------------------------------------------------------------------%

@@ -309,6 +353,18 @@
          Cs = [A | Cs0]
      ).

+assoc_list.foldl_keys_only(_, [], !Acc).
+assoc_list.foldl_keys_only(P, [KV | KVs], !Acc) :-
+    KV = K - _V,
+    P(K, !Acc),
+    assoc_list.foldl_keys_only(P, KVs, !Acc).
+
+assoc_list.foldl_values_only(_, [], !Acc).
+assoc_list.foldl_values_only(P, [KV | KVs], !Acc) :-
+    KV = _K - V,
+    P(V, !Acc),
+    assoc_list.foldl_values_only(P, KVs, !Acc).
+
  %-----------------------------------------------------------------------------%
  :- end_module assoc_list.
  %-----------------------------------------------------------------------------%

--------------------------------------------------------------------------
mercury-reviews mailing list
Post messages to:       mercury-reviews at csse.unimelb.edu.au
Administrative Queries: owner-mercury-reviews at csse.unimelb.edu.au
Subscriptions:          mercury-reviews-request at csse.unimelb.edu.au
--------------------------------------------------------------------------



More information about the reviews mailing list