6

I am using common-lisp for my real-time graphics experiments and so far it has being great. My requirements for speed and easy compatibility with cffi mean I am using 'typed' arrays. The one area of the code which really feels ugly is the generic versions of my matrix and vector math functions. As CLOS cant specialize on length of an array I am doing something like this:

(defun v+ (vec-a vec-b)
   (%v+ vec-a vec-b (length a) (length b)))

(defmethod %v+ (va vb (la (eql 3)) (lb (eql 3)))
   ***CODE HERE***)

This works but doesn't feel right. I have seen extensions to various CL implementations and heard about the promise of MOP.

I have steered away from this as I feared it would break functionality with some CL implementations but I have more recently seen the Closer-to-Mop project.

Core Question: Does MOP provide a more efficient method for specializing on length? Are there any area/techniques I should focus on?

Baggers
  • 3,183
  • 20
  • 31
  • why would you use CLOS when the GF does no dispatching... – Rainer Joswig Oct 02 '13 at 20:06
  • sorry v+ should have been defun rather than defmethod. Other than that I dont think I understand your question. %v+ is generic to handle the various lengths of vector as I commented to Menschenkindlein the recommendation came from a SO question http://stackoverflow.com/questions/11996360/common-lisp-generic-function-specializing-on-array-length. My only reason for asking this is to find out whether MOP allows for specialising on an array of a given length. maybe I should remove the background section of the question as it is not the point, just an example of a use case. – Baggers Oct 03 '13 at 07:44
  • 2
    It makes little sense to use CLOS and the MOP to write functions for just three vector types. Since you mention 'speed' I would use functions which can be inlined. If I'd need some short way of writing this stuff, I would write a macro for that... – Rainer Joswig Oct 03 '13 at 11:54
  • Your aboslutely right, and I do that. All the core vector library is just inlineable functions. However for speed of experimentation I also have the generic versions. which call the length specific versions. I really appreciate the help in getting in the correct direction. I guess I should just clean my code up a bit and then read AMOP as a unrelated project. – Baggers Oct 03 '13 at 12:14
  • This question seems a bit daft now. I really just need to go read. Thanks to everyone for the suggestions though. – Baggers Oct 03 '13 at 16:24

1 Answers1

0

Your code feels right for me, and what you are using is type tagging.

(defmethod v+ (vec-a vec-b)
   (labels ((find-tag (vec)
               (if (> (length vec) 3)
                   :more-than-3
                   :less-than-4)))
      (%v+ vec-a vec-b (find-tag a) (find-tag b)))

(defmethod %v+ (va vb (va-tag (eql :less-than-4)) (vb-tag (eql :less-than-4)))
   ***CODE HERE***)
  • Cheers, yeah it works just fine and in fact it was the method from one of my old questions here http://stackoverflow.com/questions/11996360/common-lisp-generic-function-specializing-on-array-length. However the question is more about how MOP increases possibilities with regards to specialising methods – Baggers Oct 02 '13 at 18:52