This is a must have. Creating a list of the links to review later instead of opening tabs.
- Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T.
My Bogus was an attempt to abstract a method by which I could get access to a variable using a syntatic-sugar idiom. The abstraction leaks. Here is how it leaks:
(should-maintain-scope-of-inner-property (with-property (value 24) (value :is (incf (value)))) (assert-equal 25 (value)))
‘incf’ is a destructive operation.
Abstraction (computer science): is a mechanism and practice to reduce and factor out details so that one can focus on a few concepts at a time.
If you read Lisp Trick #1, hopefully, you saw a pretty good abstraction. What it was doing was abstracting out the setting and getting a value from a variable. As a bonus, because of the way &key parameter keyword can be used, we were able to differentiate whether you were getting the value or setting the variable. Another bonus is that the variable we are getting and setting is encapsulated.
Usually an abstraction is created with the use of macros. Now I’ve made an even larger abstraction of the setter/getter abstraction.
- WHY? When using my own home grown object system, I found out that it could not handle keyword parameters.
- Problem. Take a parameter list and remove keyword parameters.
- Solution. Parse the argument list to the functions, remove and if needed replace keyword.
- Example. Need to remove &key and replace with keyword and variable.
- (a b) => (a b)
- (a (b)) => (a (b))
- (&key a) => (:a a)
- (&key a b) => (:a a :b b)
- (a &key b) => (a :b b)
- (a (&key b)) => (a (:b b))
How many times have you created two function for setting a specific value and then getting the value?
(let ((value "")) (defun property (&key is) (cond (is (setf value is)) (t value))) ;;; here is getter and setter functions (defun get-property () value) (defun set-property (data) (setf value data))) (define-test get_set-property (assert-equal "" (get-property)) (assert-equal "property" (set-property "property")) (assert-equal "property" (get-property)) (assert-equal "" (set-property ""))) (define-test property (assert-equal "" (property)) (assert-equal "property" (property :is "property")) (assert-equal "property" (property)) (assert-equal "" (property :is ""))
Using a &key keyword parameter adds syntatic sugar to setting properties. It also means less typing.
It is done with a conditional. Another benefit is that behavior for the property is located in only one function instead of two functions.
New Use Case
(Zach Beane’s Request)
Need to be able to set the property to nil. Added the test to my unit tests. It failed. Modified the code and now it passes.
You just need to add a supplied-p parameter.
(let ((value "")) (defun property (&key (is nil is-p)) (cond (is-p (setf value is)) (t value)))) (define-test property (assert-equal "" (property)) (assert-equal "property" (property :is "property")) (assert-equal "property" (property)) (assert-equal "" (property :is "")) (assert-equal nil (property :is nil)))
Happy Lisp Cargo-Culting!
I ran across this post, Prototyping Genetic Algorithms in Lisp.
Check it out. It looks to be more along the lines of Genetic Programming than Genetic Algorithms.
His code looks really good. Very readable! The methods are small, a sign of well factored code.
References for our discussion:
- The power of abstraction, reuse and simplicity: an object-oriented library for event-driven design. Most of the code implemented for a EDA is derived from this document.
- Graphic Objects. Lisp example for EDA. Specifically Graphic User Interfaces.
- Big, Complex, and Tested? Just Say ‘When’. Ruby example. Follows closely Bertrand Meyer’s document for the implementation of the
Requirements for our EDA:
- Storage of events to be executed when events triggered.
- Push/Pull Model.
- Broadcast of events. One trigger can execute more than one handler.
- Ex. When User Clicks Save Button, data is saved and the event is logged as executed.