caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: "Daniel Bünzli" <daniel.buenzli@erratique.ch>
To: Ivan <ivg@ieee.org>
Cc: caml-list@inria.fr
Subject: Re: [Caml-list] howto: recursively iterate over filesystem
Date: Tue, 3 Jul 2012 14:14:59 +0200	[thread overview]
Message-ID: <B7F0B3BD42684530A51B103D8A5B8757@erratique.ch> (raw)
In-Reply-To: <271211341286221@web29f.yandex.ru>

Hello, 

The book on Unix system programming in OCaml shows how to write a `find` function. 

    http://ocamlunix.forge.ocamlcore.org/files.html#htoc11

But if you don't need tight control over the process and don't want to link against the Unix module, just roll your own with the functions in Sys. 

That's what I usually use (note that it does follow symlinks). 

    val fold_files_rec : string list -> ('a -> string -> 'a) -> 'a -> 'a
    (** [fold_files_rec dirs f acc] lists the files in [dirs], recursively
         in depth first order and folds the function [f] over the file names. *)

    let fold_files_rec dirs f acc = 
      let readdir d = try Array.to_list (Sys.readdir d) with Sys_error _ -> [] in
      let is_dir d = try Sys.is_directory d with Sys_error _ -> false in
      let rec loop f acc = function
      | (d :: ds) :: up -> 
         let files = List.rev (List.rev_map (Filename.concat d) (readdir d)) in
         let dirs, files = List.partition is_dir files in
         let acc = List.fold_left f acc files in 
         loop f acc (dirs :: ds :: up)
      | [] :: [] -> acc
      | [] :: up -> loop f acc up
      | _ -> assert false
      in
      loop f acc (dirs :: []) 

Best,

Daniel

      parent reply	other threads:[~2012-07-03 12:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-07-03  3:30 Ivan
2012-07-03  3:42 ` Rudi Grinberg
2012-07-03  6:19 ` Stéphane Glondu
2012-07-03  8:21   ` Ivan
2012-07-03  8:24     ` Gabriel Scherer
2012-07-03  8:33       ` Ivan
2012-07-03  8:45         ` Adrien
2012-07-03  8:46       ` Fabrice Le Fessant
2012-07-03  9:25         ` Ivan
2012-07-03 12:04           ` Eric Cooper
2012-07-03 12:28             ` Daniel Bünzli
2012-07-03 19:50     ` [Caml-list] " Sylvain Le Gall
2012-07-03 12:14 ` Daniel Bünzli [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=B7F0B3BD42684530A51B103D8A5B8757@erratique.ch \
    --to=daniel.buenzli@erratique.ch \
    --cc=caml-list@inria.fr \
    --cc=ivg@ieee.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).