Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6666529
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:50:17+00:00 2026-05-26T02:50:17+00:00

I’m converting some Scheme code to Clojure. The original uses a dispatching pattern that’s

  • 0

I’m converting some Scheme code to Clojure. The original 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?)

Later, when the generic function “merge” is called, each handler is asked if it works on the operands.

As I understand multimethods, the dispatch function is defined across the set of implementations, with dispatch to a specific method based on the return value of the dispatch-fn. In the Scheme above, new assign-operation functions can define predicates arbitrarily.

What would be an equivalent, idiomatic construct in Clojure?

EDIT: The code above comes from “The Art of the Propagator”, by Alexey Radul and Gerald Sussman.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:50:18+00:00Added an answer on May 26, 2026 at 2:50 am

    You can do this with Clojure’s multimethods fairly easily – the trick is to create a dispatch function that distinguishes between the different sets of predicates.

    The easiest way to do this is probably just to maintain a vector of “composite predicates” that apply all of the individual predicates to the full argument list, and use the index of this vector as the dispatch value:

    (def pred-list (ref []))
    
    (defn dispatch-function [& args] 
      (loop [i 0]
        (cond
          (>= i (count @pred-list))     (throw (Error. "No matching function!"))
          (apply (@pred-list i) args)   i
          :else                         (recur (inc i)))))
    
    (defmulti handler dispatch-function)
    
    (defn assign-operation [function & preds]    
      (dosync
        (let [i (count @pred-list)]
          (alter pred-list conj   
                 (fn [& args] (every? identity (map #(%1 %2) preds args))))
          (defmethod handler i [& args] (apply function args)))))  
    

    Then you can create operations to handle whatever predicates you like as follows:

    (assign-operation (fn [x] (/ x 2)) even?)
    
    (assign-operation (fn [x] (+ x 1)) odd?)
    
    (take 15 (iterate handler 77))
    => (77 78 39 40 20 10 5 6 3 4 2 1 2 1 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
Seemingly simple, but I cannot find anything relevant on the web. What is the
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.