code_junkie

Function persistence in Common Lisp

Nov 12th, 2011
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1. cl-user(1): (compile (defun hello () (format t "~&Hello~%")))
  2. hello
  3. nil
  4. nil
  5. cl-user(2): (excl:fasl-write (symbol-function 'hello) "/tmp/hello.fasl")
  6. t
  7. cl-user(3): (excl:fasl-read "/tmp/hello.fasl")
  8. (#<Function hello @ #x1000a964d2>)
  9.  
  10. (defvar *anon*)
  11.  
  12. (defun save-anonymous-function (fname args body)
  13. (let ((fname (make-pathname :type "LISP" :case :common :defaults fname)))
  14. (with-open-file (src fname :direction :output
  15. :if-does-not-exist :create :if-exists :supersede)
  16. (print `(defparameter *anon* (lambda ,args ,body)) src))
  17. (compile-file fname)))
  18.  
  19. (defun load-anonymous-function (fname)
  20. (let ((*load-verbose* nil)
  21. (*anon* nil)) ; to avoid modifying the global one.
  22. (load fname)
  23. *anon*))
Add Comment
Please, Sign In to add comment