Reputation: 49329
i am trying to traverse a given directory and create a list of files ending in .jpg.
(setq files (list ))
(defun jpg-list(directory)
(dolist (node (directory-files directory t ) )
(if (file-directory-p node)
(if (not
(string= (substring node (- (string-width node) 1)) "."))
(jpg-list node))
(if (string-match ".jpg" node)
(setq files (cons node files)))))
files)
i would like to do this without using an external variable (files). What is the idiomatic way of doing this in elisp?
Upvotes: 1
Views: 1003
Reputation: 411
Here's my functional solution:
(defun jpg-list (dir)
(if (file-directory-p dir)
(append
(directory-files dir t ".jpg$")
(apply 'append (mapcar 'jpg-list (directory-files dir t "[^.]+"))))))
Upvotes: 4
Reputation: 74430
Your best bet is use 'directory-files
full interface and just use:
(directory-files directory t ".jpg$")
The bigger question was how to avoid the top-level variable, which is usually solved by a 'let
statement, e.g.
(defun jpg-list (directory)
(let ((files))
...do something with files...
files))
Upvotes: 5