Skip to content

Commit

Permalink
[stdune] Add Map.interruptible_fold
Browse files Browse the repository at this point in the history
The implementation is quite hackish and should be upstreamed;
suggestions welcome.

Signed-off-by: Emilio Jesus Gallego Arias <[email protected]>
  • Loading branch information
ejgallego committed Feb 4, 2020
1 parent 79a27e5 commit 3a729f8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/stdune/map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,26 @@ module Make (Key : Key) : S with type key = Key.t = struct

let fold t ~init ~f = foldi t ~init ~f:(fun _ x acc -> f x acc)

type ('a, 'b) interruptible_fold_step =
| Continue of 'a
| Break of 'b

let interruptible_fold (t : 'a t) ~(init : 'b) ~(f: 'a -> 'b -> ('b, 'c) interruptible_fold_step) : ('b, 'c) interruptible_fold_step =
let exception Break_fold in
let break_data = ref None in
let f data acc =
match f data acc with
| Continue acc -> acc
| Break res ->
break_data := Some res;
raise Break_fold
in
try
Continue (fold t ~init ~f)
with
| Break_fold ->
Break (Option.value_exn !break_data)

let for_alli t ~f = for_all t ~f

let for_all t ~f = for_alli t ~f:(fun _ x -> f x)
Expand Down
11 changes: 11 additions & 0 deletions src/stdune/map_intf.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ module type S = sig

val foldi : 'a t -> init:'b -> f:(key -> 'a -> 'b -> 'b) -> 'b

(* At some point this should be moved to [List] *)
type ('a, 'b) interruptible_fold_step =
| Continue of 'a
| Break of 'b

val interruptible_fold
: 'a t
-> init:'b
-> f:('a -> 'b -> ('b, 'c) interruptible_fold_step)
-> ('b, 'c) interruptible_fold_step

val for_all : 'a t -> f:('a -> bool) -> bool

val for_alli : 'a t -> f:(key -> 'a -> bool) -> bool
Expand Down

0 comments on commit 3a729f8

Please sign in to comment.