Isoraqathedh

Ensure book specificity

Feb 13th, 2016
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.90 KB | None | 0 0
  1. (defgeneric ensure-book-specific (book specificity)
  2.   (:documentation "Ensures that the book is at least as specific as specificity.
  3. Raises appropriate errors and gives common resolutions.")
  4.   (:method ((book-obj page) specificity)
  5.     (let ((specificity-spec (specificities book-obj))
  6.           (page-number-list (page-numbers book-obj)))
  7.  
  8.       ;; Catch spec errors.
  9.       (unless (member specificity specificity-spec)
  10.         (restart-case (error 'no-such-specificity
  11.                              :datum specificity :target specificity-spec)
  12.           (use-value (return-value)
  13.             :interactive (lambda ()
  14.                            (format *query-io* "Write the return value: ")
  15.                            (list (read)))
  16.             :report "Specify the return value for this function."
  17.             (return-from ensure-book-specific return-value))))
  18.  
  19.       ;; Zip through the list to check for discrepancies and allow fixing.
  20.       (loop for test-specificity in specificity-spec
  21.             for test-number = page-number-list then (cdr test-number)
  22.             for list-position from 1
  23.             unless (car test-number)
  24.             do (restart-case (error 'not-specific-enough
  25.                                     :current-specificity test-specificity
  26.                                     :target-specificity specificity)
  27.                  (store-value (new-value)
  28.                    :interactive (lambda ()
  29.                                   (format *query-io*
  30.                                           "Write in a new value for ~a: "
  31.                                           test-specificity)
  32.                                   (list (parse-integer (read-line))))
  33.                    :report "Provide a value for the missing specificity."
  34.                    (setf (page-numbers book-obj)
  35.                          (loop for i from 1 to list-position
  36.                                for j = page-number-list then (cdr j)
  37.                                if (or (cdr j) (car j)) collect (car j)
  38.                                else collect (if (= i list-position) new-value)))
  39.                    (ensure-book-specific book-obj specificity))
  40.                  (store-all-values (new-values)
  41.                    :report "Put in new values of book/page/subpage all at once."
  42.                    :interactive (lambda ()
  43.                                   (format *query-io*
  44.                                           "Write in order the values of ~a: "
  45.                                           specificity-spec)
  46.                                   (list (read)))
  47.                    (setf (page-numbers book-obj) new-values)
  48.                    (ensure-book-specific book-obj test-specificity)))
  49.             when (eql test-specificity specificity)
  50.             do (when (cdr test-number)
  51.                  (warn "~s still remains in page-number list!"
  52.                        (cdr test-number)))
  53.             and return book-obj))))
Advertisement
Add Comment
Please, Sign In to add comment