具有多个条件的如果不删除
Remove-if-not with multiple conditions
我想查找所有以 .jpg、.png 或 .jpeg 结尾的文件。
我这样写:
(defun get-picture (dir)
(remove-if-not (lambda (item)
(or (string= ".jpg" (pathname-type item))
(string= ".png" (pathname-type item))
(string= ".jpeg" (pathname-type item))))
(uiop:directory-files dir)))
但我觉得不太好。例如,当您想搜索更多内容时。所以我这样写:
(defun search-file (dir file-types)
(remove-if-not (lambda (item)
(mapc (lambda (type)
(string= type (pathname-type item)))
file-types))
(uiop:directory-files dir)))
但显然,这里的 mapc 不正确。
所以我想知道有没有更好的方法(除了 dolist)?
(defun get-picture-files (d &key
(extensions '("jpg" "png" "jpeg"))
(test #'string-equal))
(remove-if (lambda (p)
(not (member (pathname-type p)
extensions
:test test)))
(uiop:directory-files d)))
这个
- 允许您指定扩展名;
- 和测试(所以“GOO.JPG”);
- 只调用
pathname-type
一次;
- 不使用已弃用的
remove-if-not
。
我想查找所有以 .jpg、.png 或 .jpeg 结尾的文件。 我这样写:
(defun get-picture (dir)
(remove-if-not (lambda (item)
(or (string= ".jpg" (pathname-type item))
(string= ".png" (pathname-type item))
(string= ".jpeg" (pathname-type item))))
(uiop:directory-files dir)))
但我觉得不太好。例如,当您想搜索更多内容时。所以我这样写:
(defun search-file (dir file-types)
(remove-if-not (lambda (item)
(mapc (lambda (type)
(string= type (pathname-type item)))
file-types))
(uiop:directory-files dir)))
但显然,这里的 mapc 不正确。 所以我想知道有没有更好的方法(除了 dolist)?
(defun get-picture-files (d &key
(extensions '("jpg" "png" "jpeg"))
(test #'string-equal))
(remove-if (lambda (p)
(not (member (pathname-type p)
extensions
:test test)))
(uiop:directory-files d)))
这个
- 允许您指定扩展名;
- 和测试(所以“GOO.JPG”);
- 只调用
pathname-type
一次; - 不使用已弃用的
remove-if-not
。