for review: assoc_list__map_from_corresponding_lists
Tyson Dowd
trd at cs.mu.OZ.AU
Thu Jan 22 17:38:18 AEDT 1998
Hi,
Here's a quick change to the library that was suggested to
help with my layouts change.
===================================================================
Estimated hours taken: 0.5
library/assoc_list.m:
Add a new predicate:
assoc_list__map_from_corresponding_lists
which applies a predicate to the corresponing list
elements before packaging them into an assoc list.
Index: library/assoc_list.m
===================================================================
RCS file: /home/staff/zs/imp/mercury/library/assoc_list.m,v
retrieving revision 1.8
diff -u -r1.8 assoc_list.m
--- assoc_list.m 1998/01/13 09:58:23 1.8
+++ assoc_list.m 1998/01/22 06:30:37
@@ -37,6 +37,15 @@
assoc_list(K,V)).
:- mode assoc_list__from_corresponding_lists(in, in, out) is det.
+ % Zip together two lists; abort if they are of different lengths.
+ % Apply a predicate to each pair before adding it to the
+ % list.
+
+:- pred assoc_list__map_from_corresponding_lists(pred(X, Y, K, V),
+ list(X), list(Y), assoc_list(K,V)).
+:- mode assoc_list__map_from_corresponding_lists(
+ pred(in, in, out, out) is det, in, in, out) is det.
+
% Return the first member of each pair.
:- pred assoc_list__keys(assoc_list(K, V), list(K)).
@@ -61,6 +70,15 @@
assoc_list(K, V)).
:- mode assoc_list__remove(in, in, out, out) is semidet.
+ % assoc_list__map(T, L, M) uses the closure T
+ % to transform the elements of L into the elements of M.
+
+:- pred assoc_list__map(pred(X, Y), list(X), list(Y)).
+:- mode assoc_list__map(pred(in, out) is det, in, out) is det.
+:- mode assoc_list__map(pred(in, out) is semidet, in, out) is semidet.
+:- mode assoc_list__map(pred(in, out) is multi, in, out) is multi.
+:- mode assoc_list__map(pred(in, out) is nondet, in, out) is nondet.
+
%-----------------------------------------------------------------------------%
:- implementation.
@@ -103,6 +121,38 @@
assoc_list__from_corresponding_2([A | As], [B | Bs], [A - B | ABs]) :-
assoc_list__from_corresponding_2(As, Bs, ABs).
+assoc_list__map_from_corresponding_lists(Pred, Xs, Ys, KVs) :-
+ ( assoc_list__map_from_corresponding_2(Pred, Xs, Ys, KVs0) ->
+ KVs = KVs0
+ ;
+ PredType = type_name(type_of(Pred)),
+ list__length(Xs, XLength),
+ string__int_to_string(XLength, XLengthString),
+ list__length(Ys, YLength),
+ string__int_to_string(YLength, YLengthString),
+ string__append_list(
+ ["assoc_list__from_corresponding_lists_map: lists have different lengths.\n",
+ "\tPredicate type: ",
+ PredType,
+ "\n\tFirst list length: ",
+ XLengthString,
+ "\n\tSecond list length: ",
+ YLengthString
+ ],
+ ErrorString),
+ error(ErrorString)
+ ).
+
+:- pred assoc_list__map_from_corresponding_2(pred(X, Y, K, V),
+ list(X), list(Y), assoc_list(K,V)).
+:- mode assoc_list__map_from_corresponding_2(pred(in, in, out, out) is det,
+ in, in, out) is semidet.
+
+assoc_list__map_from_corresponding_2(_Pred, [], [], []).
+assoc_list__map_from_corresponding_2(Pred, [X | Xs], [Y | Ys], [A - B | ABs]) :-
+ call(Pred, X, Y, A, B),
+ assoc_list__map_from_corresponding_2(Pred, Xs, Ys, ABs).
+
assoc_list__keys([], []).
assoc_list__keys([K - _ | KVs], [K | Ks]) :-
assoc_list__keys(KVs, Ks).
--
Tyson Dowd #
# Surreal humour isn't eveyone's cup of
trd at cs.mu.oz.au # fur.
http://www.cs.mu.oz.au/~trd #
More information about the developers
mailing list