如何使用 LispWorks 的 fast-directory-files 和 fdf-handle-* 函数?

How to use LispWorks' fast-directory-files with fdf-handle-* functions?

LispWorks 的 fast-directory-filesfdf-handle-* 函数看起来很有前途。 LispWorks 手册中的 Fast access to files in a directory 页说,

fast-directory-files gives a faster way to access files than directory, especially in situations when you need to filter based on simple features such as size and access time, or filter based on the name in a more complex way than directory can.

我试图了解如何使用它们,但我遇到了文档中的一些不透明问题,而且我对 Common Lisp 还很陌生。

举个例子,人们希望使用 fdf-handle-size 获取文件的文件大小。 detailed manual page 没有提供示例,文字似乎有点简洁。例如,

The fdf-handle can be accessed by the following readers. Functions named in parentheses would return the same value when called on the full path of the file:

fdf-handle-size returns the size of the file in bytes.

[etc.]

注释说,

The fdf-handle can be used only within the dynamic scope of the callback to which it was passed.

通过反复试验我得出了这一点(当然是错误的):

CL-USER 1 > (let (file-size)
  (fast-directory-files "/temp/a.txt"
                        #'(lambda (path handle)
                           (push (fdf-handle-size handle) file-size))))
("save.lisp" "a.txt" "a.lisp")

如何获得 /temp/a.txt 的文件大小?更重要的是,通常应该如何将 LispWorks 的 fast-directory-filesfdf-handle-* 函数一起使用?

上面的代码段没有 return 回调的输出。因此

(let (file-size)
  (fast-directory-files "/temp/a.txt"
                        #'(lambda (path handle)
                           (push (fdf-handle-size handle) file-size)))
  file-size)

将生成目录中所有文件的文件大小列表。

另请注意,fast-directory-files 接受 dir-pathname - 目的是处理目录中的所有文件,因此 a.txt 是忽略。

如果您只需要单个文件的大小 - 打开它并使用 file-length(当然要注意元素大小)。