From: fortunatus on 29 Jan 2010 14:18 On Jan 29, 11:27 am, piscesboy <oraclmas...(a)gmail.com> wrote: > When is it more advantageous to write your own clojures encapsulating > data and functions than to use the CLOS system? Consider it a matter of convenience - when you only have one function, closures are pretty easy. (I use 'em sometimes.) However what about when you have half dozen functions you want to associate with a certain context? With a closure you'll have to keep it organized with a dispatch function or with multiple value return/assignment. I find CLOS becomes clearer and easier in that case.
From: Pillsy on 29 Jan 2010 15:32 On Jan 29, 11:27 am, piscesboy <oraclmas...(a)gmail.com> wrote: > When is it more advantageous to write your own clojures encapsulating > data and functions than to use the CLOS system? If you find yourself writing something like this... (defun make-knight (name quest favorite-color) (lambda (message) (ecase message (:get-name name) (:get-quest quest) (:get-favorite-color favorite-color)))) (defun send (message object) (funcall object message)) ....it's definitely time to start using class instances. Cheers, Pillsy
From: Raymond Wiker on 29 Jan 2010 16:37 "Frode V. Fjeld" <frode(a)netfonds.no> writes: > piscesboy <oraclmaster(a)gmail.com> writes: > >> On Jan 29, 11:27 am, piscesboy <oraclmas...(a)gmail.com> wrote: >>> When is it more advantageous to write your own clojures encapsulating >>> data and functions than to use the CLOS system? > > I'd say "never" is a fairly good approximation. ... but not quite. If you need the extra performance, don't need the bells & whistles, and the use-case is simple enough, then closures may be a better match. As an example, this is a (simple!) implementation of the RC4 cipher which is about 3 times faster than the clos-based implementation I started with: (defun make-generator (key) (let ((state (make-array '(256) :element-type 'fixnum))) (loop for i below 256 do (setf (aref state i) i)) (loop for i below 256 with j = 0 with key-length = (length key) do (progn (setf j (logand (+ j (char-code (char key (mod i key-length))) (aref state i)) 255)) (rotatef (aref state i) (aref state j)))) (let ((i 0) (j 0)) (lambda () (setf i (logand (1+ i) 255)) (setf j (logand (+ j (aref state i)) 255)) (rotatef (aref state i) (aref state j)) (aref state (logand (+ (aref state i) (aref state j)) 255)))))) (defun l-cipher (key text &optional (discard-count 0)) (let ((generator (make-generator key))) (loop for i below discard-count do (funcall generator)) (with-output-to-string (s) (loop for c across text do (write-char (code-char (logxor (char-code c) (funcall generator))) s)) s))) >> ooops...I meant to say "closures". I'm drunk. Sorry. > > Don't worry, we're tolerant of speech impediments here.
From: Pascal Costanza on 29 Jan 2010 18:17 On 29/01/2010 17:27, piscesboy wrote: > When is it more advantageous to write your own clojures encapsulating > data and functions than to use the CLOS system? Closures are more flexible with regard to capturing state: You don't need to anticipate in advance all the kinds of data you want to capture, and you don't need to make sure that all assignments to the relevant parts go through some specific writer functions. If you change your mind about details in these regards, it's a lot less work because the changes are typically local and ad hoc, you normally don't even notice that you changed something in that regard. A CLOS-based solution is more flexible with regard to the functionality you can provide to clients: Instead of single anonymous closures, you can provide multiple named functions that the client can select from, and in the case of CLOS, the dispatch to the actual method can be based on several arguments, which can get progressively cumbersome with just closures. However, a CLOS-based solution requires more planning and structuring which, depending on situation, can increase clarity or annoyance, or both. Pascal -- My website: http://p-cos.net Common Lisp Document Repository: http://cdr.eurolisp.org Closer to MOP & ContextL: http://common-lisp.net/project/closer/
From: "Dimiter "malkia" Stanev" on 29 Jan 2010 18:41 Tim Bradshaw wrote: > On 2010-01-29 16:27:18 +0000, piscesboy said: > >> When is it more advantageous to write your own clojures encapsulating >> data and functions than to use the CLOS system? > > On Thursday afternoons. Sometimes also on Saturdays if the moon is > full. Do not attempt this without suitable protective clothing. Tonight! http://www.space.com/spacewatch/biggest-full-moon-2010-100129-tm.html
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Concatenating adjacent strings in a list Next: CL to generate native docs |