2009/03/31

SERIES の encapsulated

Common Lisp の SERIES with-open-file 等の unwind-protect を使う場合はencapsulated を使うといいらしい?

encapsulated の第二引数で使えるのは SCAN-FN, SCAN-FN-INCLUSIVE, COLLECT-FNのいずれか。

よくわかってない。。。詳細は SERIES に添付の s-doc.txt を参照。

(require :series)
(use-package :series)

(defun scan-file-wrap (file name body)
`(with-open-file (,file ,name :direction :input) ,body))

(defmacro simple-scan-file (name)
(let ((file (gensym)))
`(encapsulated #'(lambda (body)
(scan-file-wrap ',file ',name body))
(scan-fn t
(lambda ()
(read ,file nil))
(lambda (x)
(read ,file nil))
#'null))))

(let ((x (simple-scan-file "/tmp/b.txt")))
(collect-sum (map-fn t (lambda (x) (* x x)) x)))

0 件のコメント: