[racket-dev] Speeding up `in-directory`

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Wed Sep 4 15:57:51 EDT 2013

One more try, to fix problems with adding relative paths and for a
starting path that isn't an existing directory:

(define (in-directory6 [orig-dir #f])
  (define init-dir (current-directory))
  ;; current state of the sequence is a list of paths to produce; when
  ;; incrementing past a directory, add the directory's immediate
  ;; content to the front of the list:
  (define (next l)
    (define d (car l))
    (define full-d (path->complete-path d init-dir))
    (if (directory-exists? full-d)
        (append (dir-list full-d d) (cdr l))
        (cdr l)))
  (define (dir-list full-d d)
    (for/list ([f (in-list (directory-list full-d))])
      (build-path d f)))
  (make-do-sequence
   (lambda ()
     (values
      car
      next
      (if orig-dir
          (dir-list (path->complete-path orig-dir init-dir) 
                    orig-dir)
          (directory-list init-dir))
      pair?
      #f
      #f))))

Posted on the dev mailing list.