Hi all,

I have some code in Scheme that I'm converting over to Clojure. It frequently 
uses a dispatching pattern that's very similar to multimethods, but with an 
inverted approach to the matching predicates.  For example, there a generic 
function "assign-operations". The precise implementation details aren't too 
important at the moment, but notice that it can take a list of 
argument-predicates.

(define (assign-operation operator handler . argument-predicates)
  (let ((record
         (let ((record (get-operator-record operator))
               (arity (length argument-predicates)))
           (if record
               (begin
                 (if (not (fix:= arity (operator-record-arity record)))
                     (error "Incorrect operator arity:" operator))
                 record)
               (let ((record (make-operator-record arity)))
                 (hash-table/put! *generic-operator-table* operator record)
                 record)))))
    (set-operator-record-tree! record
                               (bind-in-tree argument-predicates
                                             handler
                                             (operator-record-tree record))))


The dispatched functions supply these predicates, one per argument in the arity 
of the function.

(assign-operation 'merge
 (lambda (content increment) content)
 any? nothing?)

(assign-operation 'merge
 (lambda (content increment) increment)
 nothing? any?)

(assign-operation 'merge
 (lambda (content increment)
   (let ((new-range (intersect-intervals content increment)))
     (cond ((interval-equal? new-range content) content)
           ((interval-equal? new-range increment) increment)
           ((empty-interval? new-range) the-contradiction)
           (else new-range))))
 interval? interval?)

I can certainly just transliterate this, but would like to make a more 
idiomatic conversion. What would be an equivalent construct in Clojure? This 
feels like a place for a clever application of multimethods, protocols, or 
types, but the dispatch model seems backwards from multimethods.

Cheers,
-Michael

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to