#| Copyright (c) 2008 Ryan White Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. CONDITIONS - quick and dirty intro/reminder Originally set up on Fire up lisp and enter (load "path-to-this-file") You should then be able to call (slave 10) which will return 10 (slave "foo") which will give you a set of restarts. Calling (master '(1 2 3 "foo")) will return just (1 2 3) (master '(1 2 3 "foo") :on-error "change" :chage-value 111) will return (1 2 3 111) (emperor '(1 2 3 "foo")) will return (1 2 3) (careless-master '(1 2 3 "foo")) will give a set of restarts (micromanaging-emperor '(1 2 3 "foo")) will return (1 2 3 1000000) Extend.Document.Publish. |# #| Define a condition - an indicator that something isn't totally and fantastically 100% right |# (define-condition slave-not-happy (error) ((given-value :initarg :given-value :reader given-value-read)) (:report (lambda (condition stream) (format stream "~A is not a number, dammit!" (given-value-read condition))))) #| This is the function at the bottom of the chain of events. When its unhappy, it uses the condition to let everyone know |# (defun slave (item) (if (numberp item) ;the test item ;if everything's good, just return the number (restart-case (error 'slave-not-happy :given-value item) ;or start this whole error handling trip (use-nil () :report "Use nil value." nil) (replace-item (new-value) :report "Replace the value" new-value) (replace-item-interactively (new-value) :report "Provide a value yourself." :interactive (lambda () (format t "~&Value to use: ") (list (eval (read)))) (slave new-value))))) ;finally - a potentially recursive call... #| This is a function up the chain of events. It handles the error spit out |# (defun master (mylist &key (on-error "ignore") (change-value 0)) (handler-bind ((slave-not-happy ;figuring out what to do with the slave-not-happy error (if (equal on-error "change") #'(lambda (c) (invoke-restart 'replace-item change-value)) ;we either do a substitution #'(lambda (c) (invoke-restart 'use-nil)) ; or we use nil ))) (let ((output '()) (tst "")) (loop for item in mylist do (setf tst (slave item)) (if (eq nil tst) () (push tst output))) (reverse output)))) #| This function just orders stuff done, and doesn't want to know about the details |# (defun emperor (list) (master list)) #| This version of master doesn't worry about errors at all. Basically, it just doesn't cater for the error |# (defun careless-master (mylist &key) (let ((output '()) (tst nil)) (loop for item in mylist do (setf tst (slave item)) (if (eq nil tst) () (push tst output))) (reverse output))) #| If the emperor wishes, he can do the managing of the unhappy slave. In a typically self-serving manner |# (defun micromanaging-emperor (list) (handler-bind ((slave-not-happy #'(lambda (c) (invoke-restart 'replace-item 1000000)))) (careless-master list)))