[MLton-commit] r6531

Vesa Karvonen vesak at mlton.org
Sun Apr 6 15:49:44 PDT 2008


Added realsTo for iterating over reals.
----------------------------------------------------------------------

U   mltonlib/trunk/com/ssh/extended-basis/unstable/detail/control/iter.sml
U   mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig

----------------------------------------------------------------------

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/detail/control/iter.sml
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/detail/control/iter.sml	2008-04-06 14:08:28 UTC (rev 6530)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/detail/control/iter.sml	2008-04-06 22:49:43 UTC (rev 6531)
@@ -84,6 +84,17 @@
    fun index ? = Fold.wrap ((0, (), 1), fn (i, (), d) =>
     fn m => fn f => (fn i => m (fn a => f (a & !i) before i := !i+d)) (ref i)) ?
 
+   val maxRealInt = Real.Math.pow (2.0, Real.fromInt Real.precision)
+
+   fun realsTo e = Fold.wrap ((0.0, (), 1.0), fn (b, (), s) => let
+      val n = (e-b)/s
+      val n = if 0.0 <= n andalso n <= maxRealInt then n else
+              if n < 0.0 then 0.0
+              else raise Domain
+   in
+      unfold (fn i => if i < n then SOME (i*s + b, i+1.0) else NONE) 0.0
+   end)
+
    fun inList s = unfold List.getItem s
    fun onList s = unfold (fn [] => NONE | l as _::t => SOME (l, t)) s
 

Modified: mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig
===================================================================
--- mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig	2008-04-06 14:08:28 UTC (rev 6530)
+++ mltonlib/trunk/com/ssh/extended-basis/unstable/public/control/iter.sig	2008-04-06 22:49:43 UTC (rev 6531)
@@ -255,6 +255,21 @@
    val integers : Int.t t
    (** {integers = up $ = [<0, 1, 2, ...>]} *)
 
+   (** == Iterating over Non-Integer Ranges == *)
+
+   val realsTo : Real.t -> (((Real.t, Unit.t, Real.t) mod,
+                             (Real.t, Unit.t, Real.t) mod,
+                             Real.t t) Fold.t, 'k) CPS.t
+   (**
+    *> realsTo b From a By s $ = [<a+0.0*s, a+1.0*s, ..., a+(n-1.0)*s>]
+    *
+    * where {n = (b-a)/s}.  If {n} is negative the sequence will be empty.
+    * If {n} cannot be represented to sufficient precision or is NaN, then
+    * {Domain} will be raised.
+    *
+    * Defaults: {From 0.0 By 1.0}
+    *)
+
    (** == Indexing == *)
 
    val index : (((Int.t, Unit.t, Int.t) mod,




More information about the MLton-commit mailing list