[m-dev.] For review: two new functions in int.m

Ralph Becket rafe at cs.mu.OZ.AU
Tue Jan 27 11:55:48 AEDT 2004


These patterns crop up often enough that I decided to add them to the
library:

Estimated hours taken: 0.2
Branches: main

library/int.m:
	Added functions fold_up/4 and fold_down/4 for iterating over
	contiguous integer ranges.

NEWS:
	Mention the new changes.

Index: NEWS
===================================================================
RCS file: /home/mercury1/repository/mercury/NEWS,v
retrieving revision 1.326
diff -u -r1.326 NEWS
--- NEWS	12 Jan 2004 12:46:40 -0000	1.326
+++ NEWS	27 Jan 2004 00:53:29 -0000
@@ -90,6 +90,9 @@
   
 Changes to the Mercury standard library:
 
+* We've added two new functions, fold_up/4 and fold_down/4, to int.m. 
+  These functions support iteration over contiguous integer ranges.
+
 * We've added a new library module, `array2d', for two-dimensional arrays.
 
 * We've added a new module, cord, for sequences with O(1) consing and
Index: library/int.m
===================================================================
RCS file: /home/mercury1/repository/mercury/library/int.m,v
retrieving revision 1.96
diff -u -r1.96 int.m
--- library/int.m	12 Dec 2003 00:15:02 -0000	1.96
+++ library/int.m	27 Jan 2004 00:49:07 -0000
@@ -233,6 +233,14 @@
 :- func int__bits_per_int = int.
 :- pred int__bits_per_int(int::out) is det.
 
+	% fold_up(F, Acc, Low, High) = list.foldl(F, Low `..` High, Acc)
+	%
+:- func int__fold_up(func(int, T) = T, T, int, int) = T.
+
+	% fold_down(F, Acc, Low, High) = list.foldr(F, Low `..` High, Acc)
+	%
+:- func int__fold_down(func(int, T) = T, T, int, int) = T.
+
 %-----------------------------------------------------------------------------%
 %-----------------------------------------------------------------------------%
 
@@ -659,3 +667,16 @@
 
 int__bits_per_int = X :-
 	int__bits_per_int(X).
+
+%-----------------------------------------------------------------------------%
+
+int__fold_up(F, A, Lo, Hi) =
+	( if Lo =< Hi then int__fold_up(F, F(Lo, A), Lo + 1, Hi) else A ).
+
+%-----------------------------------------------------------------------------%
+
+int__fold_down(F, A, Lo, Hi) =
+	( if Lo =< Hi then int__fold_down(F, F(Hi, A), Lo, Hi - 1) else A ).
+
+%-----------------------------------------------------------------------------%
+%-----------------------------------------------------------------------------%
--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list