diff: list__map_filter/4

Fergus Henderson fjh at cs.mu.oz.au
Tue May 20 15:51:44 AEST 1997


Hi,

The following change is in response to Warwick's mail to mercury-bugs.
Any objections?

library/list.m:
	Add list__filter_map/4, for consistency (previously we had
	list__filter/{3,4} and list__filter_map/3 but no list__filter_map/4).

Index: list.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/list.m,v
retrieving revision 1.67
diff -u -r1.67 list.m
--- list.m	1997/05/20 01:52:46	1.67
+++ list.m	1997/05/20 05:42:24
@@ -373,6 +373,14 @@
 :- pred list__filter_map(pred(X, Y), list(X), list(Y)).
 :- mode list__filter_map(pred(in, out) is semidet, in, out) is det.
 
+	% list__filter_map(Transformer, List, TrueList, FalseList) takes
+	% a predicate with one input argument and one output argument.
+	% It is called with each element of List. If a call succeeds,
+	% then the output is included in TrueList; otherwise, the failing
+	% input is included in FalseList.
+:- pred list__filter_map(pred(X, Y), list(X), list(Y), list(X)).
+:- mode list__filter_map(pred(in, out) is semidet, in, out, out) is det.
+
 	% list__sort(Compare, Unsorted, Sorted) is true iff Sorted is a
 	% list containing the same elements as Unsorted, where Sorted is
 	% a sorted list, with respect to the ordering defined by the predicate
@@ -893,6 +901,16 @@
 	),
 	list__filter_map(P, T0, L1).
 
+list__filter_map(_, [], [], []).
+list__filter_map(P, [H0|T0], L, M) :-
+        ( call(P, H0, H) ->
+                L = [H|L1],
+		M = M1
+        ;
+                L = L1,
+		M = [H0|M1]
+        ),
+        list__filter_map(P, T0, L1, M1).
 
 list__sort_and_remove_dups(P, L0, L) :-
 	list__sort(P, L0, L1),

-- 
Fergus Henderson <fjh at cs.mu.oz.au>   |  "I have always known that the pursuit
WWW: <http://www.cs.mu.oz.au/~fjh>   |  of excellence is a lethal habit"
PGP: finger fjh at 128.250.37.3         |     -- the last words of T. S. Garp.



More information about the developers mailing list