[plt-scheme] directory-list order?

From: Eli Barzilay (eli at barzilay.org)
Date: Wed Sep 13 12:21:19 EDT 2006

On Sep 13, Matthias Felleisen wrote:
> Just sort it. -- Matthias
> 
>    (require (lib "list.ss"))
> 
>    (define (read-directory)
>      (define (path< p q)
>        (< (file-or-directory-modify-seconds (path->string p))
>           (file-or-directory-modify-seconds (path->string q))))
>      (quicksort (directory-list) path<))
> 
> I know. We could use shorter names.

A few comments on this:

1. There's no need to turn the paths into strings.

2. `sort' is better to use than `quicksort'.

3. For stuff like that it can be better to cache the FS operations so
   you do only a linear number of them:

     (define (read-directory)
       (map cdr
            (sort (map (lambda (p)
                         (cons (file-or-directory-modify-seconds p) p))
                       (directory-list))
                  (lambda (p q) (< (car p) (car q))))))

   IIRC, in Ruby this hack even has a name (which I don't remember)
   and its own syntactic construct.

-- 
          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                  http://www.barzilay.org/                 Maze is Life!


Posted on the users mailing list.