add CCIO.File.walk_iter

This commit is contained in:
Simon Cruanes 2021-08-19 10:14:30 -04:00
parent 4783c635fd
commit 8e924c98be
No known key found for this signature in database
GPG key ID: 4AC01D0849AA62B6
2 changed files with 12 additions and 1 deletions

View file

@ -3,6 +3,7 @@
(** {1 IO Utils} *) (** {1 IO Utils} *)
type 'a iter = ('a -> unit) -> unit
type 'a or_error = ('a, string) result type 'a or_error = ('a, string) result
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
@ -33,6 +34,10 @@ let gen_of_array arr =
Some x Some x
) )
let rec gen_iter f g = match g() with
| None -> ()
| Some x -> f x; gen_iter f g
let gen_flat_map f next_elem = let gen_flat_map f next_elem =
let state = ref `Init in let state = ref `Init in
let rec next() = let rec next() =
@ -386,6 +391,7 @@ module File = struct
*) *)
let walk_seq d = seq_of_gen_ (walk d) let walk_seq d = seq_of_gen_ (walk d)
let walk_iter d = fun yield -> gen_iter yield (walk d)
let walk_l d = let walk_l d =
let l = ref [] in let l = ref [] in

View file

@ -31,7 +31,7 @@
- Note that the lifetime of an IO generator is tied to the underlying - Note that the lifetime of an IO generator is tied to the underlying
channel. In the example above, [chunks] must be used in the scope of [ic]. channel. In the example above, [chunks] must be used in the scope of [ic].
This will raise an error: This will raise an error:
{[ {[
# CCIO.( # CCIO.(
@ -53,6 +53,7 @@
*) *)
type 'a or_error = ('a, string) result type 'a or_error = ('a, string) result
type 'a iter = ('a -> unit) -> unit
type 'a gen = unit -> 'a option type 'a gen = unit -> 'a option
(** See [Gen] in the {{: https://github.com/c-cube/gen} gen library}. *) (** See [Gen] in the {{: https://github.com/c-cube/gen} gen library}. *)
@ -250,6 +251,10 @@ module File : sig
symlinks, etc.) symlinks, etc.)
@raise Sys_error in case of error (e.g. permission denied) during iteration. *) @raise Sys_error in case of error (e.g. permission denied) during iteration. *)
val walk_iter : t -> walk_item iter
(** Like {!walk} but with an imperative iterator.
@since NEXT_RELEASE *)
val walk_l : t -> walk_item list val walk_l : t -> walk_item list
(** Like {!walk} but returns a list (therefore it's eager and might (** Like {!walk} but returns a list (therefore it's eager and might
take some time on large directories). take some time on large directories).