Skip to content

Commit cd2d47f

Browse files
jberdinec-cube
authored andcommitted
Switch exceptions used for control flow from global to local
Signed-off-by: Josh Berdine <[email protected]>
1 parent f9c6c07 commit cd2d47f

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

src/Iter.ml

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,8 @@ let sort ?(cmp = Stdlib.compare) seq =
330330
let l = List.fast_sort cmp l in
331331
fun k -> List.iter k l
332332

333-
exception Exit_sorted
334-
335333
let sorted ?(cmp = Stdlib.compare) seq =
334+
let exception Exit_sorted in
336335
let prev = ref None in
337336
try
338337
seq (fun x ->
@@ -556,9 +555,8 @@ let diff (type a) ?(eq = ( = )) ?(hash = Hashtbl.hash) c1 c2 =
556555
c2 (fun x -> Tbl.replace tbl x ());
557556
fun yield -> c1 (fun x -> if not (Tbl.mem tbl x) then yield x)
558557
559-
exception Subset_exit
560-
561558
let subset (type a) ?(eq = ( = )) ?(hash = Hashtbl.hash) c1 c2 =
559+
let exception Subset_exit in
562560
let module Tbl = Hashtbl.Make (struct
563561
type t = a
564562
@@ -630,9 +628,8 @@ let sumf seq : float =
630628
sum := t);
631629
!sum
632630
633-
exception ExitHead
634-
635631
let head seq =
632+
let exception ExitHead in
636633
let r = ref None in
637634
try
638635
seq (fun x ->
@@ -646,9 +643,8 @@ let head_exn seq =
646643
| None -> invalid_arg "Iter.head_exn"
647644
| Some x -> x
648645
649-
exception ExitTake
650-
651646
let take n seq k =
647+
let exception ExitTake in
652648
let count = ref 0 in
653649
try
654650
seq (fun x ->
@@ -657,9 +653,8 @@ let take n seq k =
657653
k x)
658654
with ExitTake -> ()
659655
660-
exception ExitTakeWhile
661-
662656
let take_while p seq k =
657+
let exception ExitTakeWhile in
663658
try
664659
seq (fun x ->
665660
if p x then
@@ -668,8 +663,6 @@ let take_while p seq k =
668663
raise_notrace ExitTakeWhile)
669664
with ExitTakeWhile -> ()
670665
671-
exception ExitFoldWhile
672-
673666
let map_while f seq k =
674667
let exception ExitMapWhile in
675668
let consume x =
@@ -683,6 +676,7 @@ let map_while f seq k =
683676
try seq consume with ExitMapWhile -> ()
684677
685678
let fold_while f s seq =
679+
let exception ExitFoldWhile in
686680
let state = ref s in
687681
let consume x =
688682
let acc, cont = f !state x in
@@ -721,28 +715,25 @@ let rev seq =
721715
let l = MList.of_iter seq in
722716
fun k -> MList.iter_rev k l
723717
724-
exception ExitForall
725-
726718
let for_all p seq =
719+
let exception ExitForall in
727720
try
728721
seq (fun x -> if not (p x) then raise_notrace ExitForall);
729722
true
730723
with ExitForall -> false
731724
732-
exception ExitExists
733-
734725
(** Exists there some element satisfying the predicate? *)
735726
let exists p seq =
727+
let exception ExitExists in
736728
try
737729
seq (fun x -> if p x then raise_notrace ExitExists);
738730
false
739731
with ExitExists -> true
740732
741733
let mem ?(eq = ( = )) x seq = exists (eq x) seq
742734
743-
exception ExitFind
744-
745735
let find_map f seq =
736+
let exception ExitFind in
746737
let r = ref None in
747738
(try
748739
seq (fun x ->
@@ -757,6 +748,7 @@ let find_map f seq =
757748
let find = find_map
758749
759750
let find_mapi f seq =
751+
let exception ExitFind in
760752
let i = ref 0 in
761753
let r = ref None in
762754
(try
@@ -790,9 +782,8 @@ let[@inline] length seq =
790782
seq (fun _ -> incr r);
791783
!r
792784
793-
exception ExitIsEmpty
794-
795785
let is_empty seq =
786+
let exception ExitIsEmpty in
796787
try
797788
seq (fun _ -> raise_notrace ExitIsEmpty);
798789
true

0 commit comments

Comments
 (0)