diff --git a/src/core/CCIO.ml b/src/core/CCIO.ml index 1ae7744d..00e914bf 100644 --- a/src/core/CCIO.ml +++ b/src/core/CCIO.ml @@ -3,6 +3,7 @@ (** {1 IO Utils} *) +type 'a iter = ('a -> unit) -> unit type 'a or_error = ('a, string) result type 'a gen = unit -> 'a option @@ -33,6 +34,10 @@ let gen_of_array arr = 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 state = ref `Init in let rec next() = @@ -386,6 +391,7 @@ module File = struct *) 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 l = ref [] in diff --git a/src/core/CCIO.mli b/src/core/CCIO.mli index 4b2f9eaf..87855941 100644 --- a/src/core/CCIO.mli +++ b/src/core/CCIO.mli @@ -31,7 +31,7 @@ - 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]. - This will raise an error: + This will raise an error: {[ # CCIO.( @@ -53,6 +53,7 @@ *) type 'a or_error = ('a, string) result +type 'a iter = ('a -> unit) -> unit type 'a gen = unit -> 'a option (** See [Gen] in the {{: https://github.com/c-cube/gen} gen library}. *) @@ -250,6 +251,10 @@ module File : sig symlinks, etc.) @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 (** Like {!walk} but returns a list (therefore it's eager and might take some time on large directories).