Module File.Fold

Utility Functions and Combinators for fold.

These functions make it easier to provide the function parameter to fold for certain use cases.

val fold : ?follow:bool -> ?xdev:bool -> 'a folder -> 'a -> string -> 'a

fold is fold.

Utllity Functions

These functions are all folder's.

val noop : 'a folder

noop simply returns its accumulator.

Invariant: ∀dir s.t. File.Is.dir dir . (fold noop x dir = x)

val cons : string list folder

cons is (fun _exn _depth x xs -> List.cons x xs).

Example: (fold cons [] ".") returns a list of all the files in the current directory, recursively, ignoring any directory traversal errors.

Combinators

These functions all return a folder.

Ignoring Both Exception Option and Depth Parameters

val elided : (string -> 'a -> 'a) -> 'a folder

elided takes a function shaped like List.cons and returns one that ignores the exn and depth parameters.

I.e. (elided f = (fun _exn _depth x xs -> f x xs))

Example: (fold (elided List.cons) [] ".")

Exception Handlers

val prerr : 'a folder -> 'a folder

(prerr f) calls folder f preceded by printing any exception via (Exn.to_string >> prerr_endline).

Example:

  • File.Fold.(fold (prerr cons) [] ".") = File.Fold.(fold (prerr (elided List.cons)) [] ".")
val err : 'a folder -> 'a folder

(err f) is folder f except any exception is raised.

Therefore, (err f) terminates the fold at the first exception.

Controlling the Depth of Recursion

val maxdepth : int -> 'a folder -> 'a folder

(maxdepth n f) only applies folder f to the current path if the depth of recursion is <= n.

N.B. every directory under dir is still traversed (i.e. there is no short-circuiting), so using maxdepth will not significantly decrease the amount of time spent in traversal.