Commits

bergsoe  committed b021b9f

Prefer BatEnum.from to BatEnum.from_while.

  • Participants
  • Parent commits e74240f

Comments (0)

Files changed (1)

File src/PaplSampler.ml

 
 exception IntRangeError of string * (int * int)
 
-let pi = 4. *. atan(1.0)
+let stop () = raise BatEnum.No_more_elements
+
+let pi = BatFloat.pi
 
 let product2 sa sb = BatEnum.combine (sa, sb)
 
 let product3 sa sb sc =
   let next () =
     match BatEnum.get sa, BatEnum.get sb, BatEnum.get sc with
-        Some a, Some b, Some c -> Some (a, b, c)
-      | _ -> None
+        Some a, Some b, Some c -> (a, b, c)
+      | _ -> stop ()
   in
-    BatEnum.from_while next
+    BatEnum.from next
 
 let pair s = product2 s s
 
   let rec next () =
     if !cnt == 0 then
       match BatEnum.get sx with
-          None -> None
+          None -> stop ()
         | Some x ->
             cnt := !n;
-            Some x
+            x
     else begin
       cnt := !cnt - 1;
       match BatEnum.get sy with
             cnt := 0;
             next ()
         | Some y ->
-            Some y
+            y
     end
-  in BatEnum.from_while next
+  in BatEnum.from next
 
 let c = PaplConstraint.reject
 
 let rec constrain_with_default sampler constr default =
   let rec next () =
     match BatEnum.get sampler with
-        None -> None
+        None -> stop ()
       | Some x ->
           if c constr x then
             match BatEnum.get default with
                 None -> next ()
-              | Some y -> Some y
+              | Some y -> y
           else
-            Some x
-  in BatEnum.from_while next
+            x
+  in BatEnum.from next
 
 let rec round_robin samplers =
   let ss = ref samplers in
   let acc = ref [] in
   let rec next () =
     match BatEnum.get !ss, !acc with
-        None, [] -> None
+        None, [] -> stop ()
       | None, _ ->
           ss := BatList.backwards !acc;
           acc := [];
               None -> next ()
             | Some x ->
                 acc := s :: !acc;
-                Some x
-  in BatEnum.from_while next
+                x
+  in BatEnum.from next
 
 (*
   The values are inserted in a balanced binary tree with each node storing the
           snd (get i) in
   let make () =
     match BatEnum.get position_sampler with
-        None -> None
-      | Some p -> Some (lookup 0 p)
+        None -> stop ()
+      | Some p -> lookup 0 p
   in
     if Array.length tree == 0 then
       invalid_arg "Empty enum of pairs given."
     else
-      BatEnum.from_while make
+      BatEnum.from make
 
 let distribute_by_weight ?rng pairs =
   distribute_by_weight_helper (PaplRandom.enum_float ?rng) pairs
     let a = 2. *. pi *. u2 in
       (rho *. cos a, rho *. sin a)
   in let next = sequence_pairs pairs in
-    BatEnum.from_while (fun () -> Some (next ()))
+    BatEnum.from next
 
 let gaussian_helper = box_muller_gaussian