[m-rev.] diff: additional folds for bags and assoc_lists.
Julien Fischer
jfischer at opturion.com
Fri Nov 22 16:43:47 AEDT 2013
Branches: master
-----------------------
Add additional fold predicates for bags and assoc_lists.
library/assoc_list.m:
Add foldl2_values/6.
library/bag.m:
Add foldl/4 and foldl2/6.
NEWS:
Announce the above additions.
Julien.
diff --git a/NEWS b/NEWS
index 4abef36..562e866 100644
--- a/NEWS
+++ b/NEWS
@@ -21,6 +21,13 @@ Changes to the Mercury standard library:
* We have added the following predicates to the list module: foldr2/6 and
foldr3/8.
+* We have added the following predicates to the bag module: foldl/4 and
+ foldl2/6.
+
+* We have added the following predicate to the assoc_list module:
+ foldl2_values/6.
+
+
NEWS for Mercury 13.05.2
------------------------
diff --git a/library/assoc_list.m b/library/assoc_list.m
index b6098ca..63d0e40 100644
--- a/library/assoc_list.m
+++ b/library/assoc_list.m
@@ -179,6 +179,27 @@
:- mode assoc_list.foldl_values(pred(in, in, out) is nondet, in,
in, out) is nondet.
+ % As above, but with two accumulators.
+ %
+:- pred assoc_list.foldl2_values(pred(V, A, A, B, B), assoc_list(K, V),
+ A, A, B, B).
+:- mode assoc_list.foldl2_values(pred(in, in, out, in, out) is det, in,
+ in, out, in, out) is det.
+:- mode assoc_list.foldl2_values(pred(in, in, out, mdi, muo) is det, in,
+ in, out, mdi, muo) is det.
+:- mode assoc_list.foldl2_values(pred(in, in, out, di, uo) is det, in,
+ in, out, di, uo) is det.
+:- mode assoc_list.foldl2_values(pred(in, in, out, in, out) is semidet, in,
+ in, out, in, out) is semidet.
+:- mode assoc_list.foldl2_values(pred(in, in, out, mdi, muo) is semidet, in,
+ in, out, mdi, muo) is semidet.
+:- mode assoc_list.foldl2_values(pred(in, in, out, di, uo) is semidet, in,
+ in, out, di, uo) is semidet.
+:- mode assoc_list.foldl2_values(pred(in, in, out, in, out) is multi, in,
+ in, out, in, out) is multi.
+:- mode assoc_list.foldl2_values(pred(in, in, out, in, out) is nondet, in,
+ in, out, in, out) is nondet.
+
%-----------------------------------------------------------------------------%
%-----------------------------------------------------------------------------%
@@ -365,6 +386,12 @@ assoc_list.foldl_values(P, [KV | KVs], !Acc) :-
P(V, !Acc),
assoc_list.foldl_values(P, KVs, !Acc).
+assoc_list.foldl2_values(_, [], !Acc1, !Acc2).
+assoc_list.foldl2_values(P, [KV | KVs], !Acc1, !Acc2) :-
+ KV = _K - V,
+ P(V, !Acc1, !Acc2),
+ assoc_list.foldl2_values(P, KVs, !Acc1, !Acc2).
+
%-----------------------------------------------------------------------------%
:- end_module assoc_list.
%-----------------------------------------------------------------------------%
diff --git a/library/bag.m b/library/bag.m
index b950d51..8266c2f 100644
--- a/library/bag.m
+++ b/library/bag.m
@@ -243,6 +243,33 @@
:- pred bag.subset_compare(comparison_result::out, bag(T)::in, bag(T)::in)
is semidet.
+ % Perform a traversal of the bag, applying an accumulator predicate
+ % to each value - count pair.
+ %
+:- pred bag.foldl(pred(T, int, A, A), bag(T), A, A).
+:- mode bag.foldl(pred(in, in, in, out) is det, in, in, out) is det.
+:- mode bag.foldl(pred(in, in, mdi, muo) is det, in, mdi, muo) is det.
+:- mode bag.foldl(pred(in, in, di, uo) is det, in, di, uo) is det.
+:- mode bag.foldl(pred(in, in, in, out) is semidet, in, in, out) is semidet.
+:- mode bag.foldl(pred(in, in, mdi, muo) is semidet, in, mdi, muo) is semidet.
+:- mode bag.foldl(pred(in, in, di, uo) is semidet, in, di, uo) is semidet.
+
+ % As above, but with two accumulators.
+ %
+:- pred bag.foldl2(pred(T, int, A, A, B, B), bag(T), A, A, B, B).
+:- mode bag.foldl2(pred(in, in, in, out, in, out) is det, in, in, out,
+ in, out) is det.
+:- mode bag.foldl2(pred(in, in, in, out, mdi, muo) is det, in, in, out,
+ mdi, muo) is det.
+:- mode bag.foldl2(pred(in, in, in, out, di, uo) is det, in, in, out,
+ di, uo) is det.
+:- mode bag.foldl2(pred(in, in, in, out, in, out) is semidet, in, in, out,
+ in, out) is semidet.
+:- mode bag.foldl2(pred(in, in, in, out, mdi, muo) is semidet, in, in, out,
+ mdi, muo) is semidet.
+:- mode bag.foldl2(pred(in, in, in, out, di, uo) is semidet, in, in, out,
+ di, uo) is semidet.
+
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
@@ -628,5 +655,13 @@ bag.subset_compare(Res, bag(A), bag(B)) :-
).
%-----------------------------------------------------------------------------%
+
+bag.foldl(Pred, bag(Bag), !Acc) :-
+ map.foldl(Pred, Bag, !Acc).
+
+bag.foldl2(Pred, bag(Bag), !Acc1, !Acc2) :-
+ map.foldl2(Pred, Bag, !Acc1, !Acc2).
+
+%-----------------------------------------------------------------------------%
:- end_module bag.
%-----------------------------------------------------------------------------%
More information about the reviews
mailing list