Work in progress, recovered from old crybaby.
[sod] / src / utilities.lisp
index 15f9091..5c061bb 100644 (file)
   `(let ((it ,cond)) (when it ,@body)))
 
 (export 'acond)
-(defmacro acond (&rest clauses &environment env)
+(defmacro acond (&body clauses &environment env)
   "Like COND, but with `it' bound to the value of the condition.
 
    Each of the CLAUSES has the form (CONDITION FORM*); if a CONDITION is
 ;;;--------------------------------------------------------------------------
 ;;; MOP hacks (not terribly demanding).
 
+(export 'instance-initargs)
+(defgeneric instance-initargs (instance)
+  (:documentation
+   "Return a plausble list of initargs for INSTANCE.
+
+   The idea is that you can make a copy of INSTANCE by invoking
+
+       (apply #'make-instance (class-of INSTANCE)
+              (instance-initargs INSTANCE))
+
+   The default implementation works by inspecting the slot definitions and
+   extracting suitable initargs, so this will only succeed if enough slots
+   actually have initargs specified that `initialize-instance' can fill in
+   the rest correctly.
+
+   The list returned is freshly consed, and you can destroy it if you like.")
+  (:method ((instance standard-object))
+    (mapcan (lambda (slot)
+             (aif (slot-definition-initargs slot)
+                  (list (car it)
+                        (slot-value instance (slot-definition-name slot)))
+                  nil))
+           (class-slots (class-of instance)))))
+
 (export '(copy-instance copy-instance-using-class))
 (defgeneric copy-instance-using-class (class instance &rest initargs)
   (:documentation
   "Composition of functions.  Functions are applied left-to-right.
 
    This is the reverse order of the usual mathematical notation, but I find
-   it easier to read.  It's also slightly easier to work with in programs."
+   it easier to read.  It's also slightly easier to work with in programs.
+   That is, (compose F1 F2 ... Fn) is what a category theorist might write as
+   F1 ; F2 ; ... ; Fn, rather than F1 o F2 o ... o Fn."
+
   (labels ((compose1 (func-a func-b)
             (lambda (&rest args)
               (multiple-value-call func-b (apply func-a args)))))