Skip to content

Instantly share code, notes, and snippets.

@lindig
Last active May 9, 2024 04:54
Show Gist options
  • Select an option

  • Save lindig/be55f453026c65e761f4e7012f8ab9b5 to your computer and use it in GitHub Desktop.

Select an option

Save lindig/be55f453026c65e761f4e7012f8ab9b5 to your computer and use it in GitHub Desktop.

Revisions

  1. lindig revised this gist Jul 27, 2016. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions dir.ml
    Original file line number Diff line number Diff line change
    @@ -4,21 +4,21 @@
    * "." and ".."
    *)
    let dir_is_empty dir =
    Array.length (Sys.readdir dir) = 0
    Array.length (Sys.readdir dir) = 0

    (** [dir_contents] returns the paths of all regular files that are
    * contained in [dir]. Each file is a path starting with [dir].
    *)
    *)
    let dir_contents dir =
    let rec loop result = function
    | f::fs when Sys.is_directory f ->
    Sys.readdir f
    |> Array.to_list
    |> List.map (Filename.concat f)
    |> List.append fs
    |> loop result
    | f::fs -> loop (f::result) fs
    | [] -> result
    in
    loop [] [dir]
    let rec loop result = function
    | f::fs when Sys.is_directory f ->
    Sys.readdir f
    |> Array.to_list
    |> List.map (Filename.concat f)
    |> List.append fs
    |> loop result
    | f::fs -> loop (f::result) fs
    | [] -> result
    in
    loop [] [dir]

  2. lindig revised this gist Jul 27, 2016. No changes.
  3. lindig created this gist Jul 27, 2016.
    24 changes: 24 additions & 0 deletions dir.ml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@


    (** [dir_is_empty dir] is true, if [dir] contains no files except
    * "." and ".."
    *)
    let dir_is_empty dir =
    Array.length (Sys.readdir dir) = 0

    (** [dir_contents] returns the paths of all regular files that are
    * contained in [dir]. Each file is a path starting with [dir].
    *)
    let dir_contents dir =
    let rec loop result = function
    | f::fs when Sys.is_directory f ->
    Sys.readdir f
    |> Array.to_list
    |> List.map (Filename.concat f)
    |> List.append fs
    |> loop result
    | f::fs -> loop (f::result) fs
    | [] -> result
    in
    loop [] [dir]