[m-rev.] For post-commit review: Add foldl2/6 predicate to array module in standard library.

Paul Bone pbone at csse.unimelb.edu.au
Fri Sep 12 20:43:01 AEST 2008


For post-commit review by anyone.

Estimated Hours Taken: 0.25
Branches: main

Add a foldl2 predicate to the array module in the standard library.

library/array.m:
    As above.

NEWS:
    Announce the change.

Index: library/array.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/array.m,v
retrieving revision 1.161
diff -u -p -r1.161 array.m
--- library/array.m	9 May 2008 06:56:04 -0000	1.161
+++ library/array.m	12 Sep 2008 10:21:40 -0000
@@ -376,6 +376,10 @@
 %:- mode array.foldl(func(in, di) = uo is det, array_ui, di) = uo is det.
 :- mode array.foldl(func(in, di) = uo is det, in, di) = uo is det.
 
+:- pred array.foldl2(pred(T1, T2, T2, T3, T3), array(T1), T2, T2, T3, T3).
+:- mode array.foldl2(pred(in, in, out, in, out) is det, in, in, out, in, out) 
+    is det.
+
     % array.foldr(Fn, Array, X) is equivalent to
     %   list.foldr(Fn, array.to_list(Array), X)
     % but more efficient.
@@ -1472,6 +1476,25 @@ foldl_0(Fn, A, X, I, Max) =
 
 % ---------------------------------------------------------------------------- %
 
+array.foldl2(P, A, X0, X, Y0, Y) :-
+    array.foldl2_0(P, A, array.min(A), array.max(A), X0, X, Y0, Y).
+
+:- pred foldl2_0(pred(T1, T2, T2, T3, T3), array(T1), int, int, T2, T2, 
+    T3, T3).
+:- mode foldl2_0(pred(in, in, out, in, out) is det, in, in, in, in, out, 
+    in, out) is det.
+
+foldl2_0(P, A, I, Max, X0, X, Y0, Y) :-
+    ( Max < I ->
+        X = X0,
+        Y = Y0
+    ;
+        P(A ^ elem(I), X0, X1, Y0, Y1),
+        foldl2_0(P, A, I+1, Max, X1, X, Y1, Y)
+    ).
+
+% ---------------------------------------------------------------------------- %
+
 array.foldr(Fn, A, X) =
     foldr_0(Fn, A, X, array.min(A), array.max(A)).
 
Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.495
diff -u -p -r1.495 NEWS
--- NEWS	11 Aug 2008 05:36:52 -0000	1.495
+++ NEWS	12 Sep 2008 10:43:01 -0000
@@ -82,6 +82,9 @@ Changes to the Mercury standard library:
 * Processes no longer terminate until all threads have finished.  Previously
   a process would terminate as soon as the original thread finished.
 
+* The following predicate has been added to the array module:
+    array.foldl2/6
+
 * The following predicates have been added to the list module:
 	list.map_corresponding/4
 	list.map2_foldl3/10

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
URL: <http://lists.mercurylang.org/archives/reviews/attachments/20080912/db04c86e/attachment.sig>


More information about the reviews mailing list