in-directory: add optional argument to filter directories

This commit is contained in:
Matthew Flatt 2013-12-24 11:41:55 -06:00
parent 3f30400a59
commit 99daa9b34d
4 changed files with 33 additions and 14 deletions

View File

@ -6,16 +6,19 @@
;; moved out of the core, while a new "base2" package can represent ;; moved out of the core, while a new "base2" package can represent
;; the new, smaller core. ;; the new, smaller core.
;; The "base" package also depends on "raclet-lib", which ensures that ;; The "base" package also depends on "racket-lib", which ensures that
;; any native libraries needed for a platform are installed along with ;; any native libraries needed for a platform are installed along with
;; practically any package installation. ;; practically any package installation.
(define collection 'multi) (define collection 'multi)
(define deps '("racket-lib")) (define deps '("racket-lib"
["racket" #:version "6.0.0.1"]))
(define implies '(core)) (define implies '(core))
(define pkg-desc "Racket libraries that are currently always available") (define pkg-desc "Racket libraries that are currently always available")
(define pkg-authors '(mflatt)) (define pkg-authors '(mflatt))
(define version "6.0.0.1")

View File

@ -361,18 +361,26 @@ each element in the sequence.
(printf "key and value: ~a\n" key+value))] (printf "key and value: ~a\n" key+value))]
} }
@defproc[(in-directory [dir (or/c #f path-string?) #f]) sequence?]{ @defproc[(in-directory [dir (or/c #f path-string?) #f]
[use-dir? ((and/c path? complete-path?) . -> . any/c)
(lambda (dir-path) #t)])
sequence?]{
Returns a sequence that produces all of the paths for files, Returns a sequence that produces all of the paths for files,
directories, and links within @racket[dir]. If @racket[dir] is not directories, and links within @racket[dir], except for the
contents of any directory for which @racket[use-dir?] returns
@racket[#f]. If @racket[dir] is not
@racket[#f], then every produced path starts with @racket[dir] as @racket[#f], then every produced path starts with @racket[dir] as
its prefix. If @racket[dir] is @racket[#f], then paths in and its prefix. If @racket[dir] is @racket[#f], then paths in and
relative to the current directory are produced. relative to the current directory are produced.
An @racket[in-directory] sequence traverses nested subdirectories An @racket[in-directory] sequence traverses nested subdirectories
recursively. To generate a sequence that includes only the immediate recursively (filtered by @racket[use-dir?]).
To generate a sequence that includes only the immediate
content of a directory, use the result of @racket[directory-list] as content of a directory, use the result of @racket[directory-list] as
a sequence. a sequence.
}
@history[#:changed "6.0.0.1" @elem{Added @racket[use-dir?] argument.}]}
@defproc*[([(in-producer [producer procedure?]) @defproc*[([(in-producer [producer procedure?])
sequence?] sequence?]

View File

@ -1,3 +1,6 @@
Version 6.0.0.1
Moved detailed-change recording to documentation
Version 6.0, January 2014 Version 6.0, January 2014
Packages & collections: ---------- Packages & collections: ----------
Reorganized collections into packages Reorganized collections into packages

View File

@ -1946,9 +1946,10 @@
(for/fold ([acc acc]) ([f (in-list (directory-list full-d))]) (for/fold ([acc acc]) ([f (in-list (directory-list full-d))])
(cons (build-path d f) acc))) (cons (build-path d f) acc)))
(define (next-body l d init-dir) (define (next-body l d init-dir use-dir?)
(let ([full-d (path->complete-path d init-dir)]) (let ([full-d (path->complete-path d init-dir)])
(if (directory-exists? full-d) (if (and (directory-exists? full-d)
(use-dir? full-d))
(dir-list full-d d (cdr l)) (dir-list full-d d (cdr l))
(cdr l)))) (cdr l))))
@ -1960,15 +1961,16 @@
(define *in-directory (define *in-directory
(case-lambda (case-lambda
[() (*in-directory #f)] [() (*in-directory #f (lambda (d) #t))]
[(orig-dir) [(orig-dir) (*in-directory #f (lambda (d) #t))]
[(orig-dir use-dir?)
(define init-dir (current-directory)) (define init-dir (current-directory))
;; current state of the sequence is a list of paths to produce; when ;; current state of the sequence is a list of paths to produce; when
;; incrementing past a directory, add the directory's immediate ;; incrementing past a directory, add the directory's immediate
;; content to the front of the list: ;; content to the front of the list:
(define (next l) (define (next l)
(define d (car l)) (define d (car l))
(next-body l d init-dir)) (next-body l d init-dir use-dir?))
(make-do-sequence (make-do-sequence
(lambda () (lambda ()
(values (values
@ -1984,16 +1986,19 @@
(λ (stx) (λ (stx)
(syntax-case stx () (syntax-case stx ()
[((d) (_)) #'[(d) (*in-directory #f)]] [((d) (_)) #'[(d) (*in-directory #f)]]
[((d) (_ dir)) [((d) (_ dir)) #'[(d) (*in-directory dir (lambda (d) #t))]]
[((d) (_ dir use-dir?-expr))
#'[(d) #'[(d)
(:do-in (:do-in
([(orig-dir) (or dir #f)] [(init-dir) (current-directory)]) ([(orig-dir) (or dir #f)]
[(init-dir) (current-directory)]
[(use-dir?) use-dir?-expr])
#true #true
([l (initial-state orig-dir init-dir)]) ([l (initial-state orig-dir init-dir)])
(pair? l) (pair? l)
([(d) (car l)]) ([(d) (car l)])
#true #true
#true #true
[(next-body l d init-dir)])]]))) [(next-body l d init-dir use-dir?)])]])))
) )