[plt-scheme] directory-list order?
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!