You can add essentially one more level of indirection to your dispatch
function logic and return an integer (or string or whatever) and match on
that.  So, with your example, when you want "method1" called, return 1 from
the dispatch function, when you want "method2" return 2, you get the idea:

(defmulti foo mclass)
(defmethod foo 1 [s] (str "I'm method 1"))
(defmethod foo 2 [s] (str "I'm method 2"))
...

Is this what you are getting at?

 - Mark

On Sun, Sep 5, 2010 at 8:50 PM, Sunil S Nandihalli <
sunil.nandiha...@gmail.com> wrote:

> Hello everybody,
>   It is awesome that we can specify our own dispatch functions and
> corresponding values.. However, I feel we should also have control over the
> functions that is used to match the value of the dispatch function with that
> of the one specified in the defmethod.
>  For instance if my dispatch function is
>
> *(defn mclass [s]*
> * (vec  (reverse (loop [ret '() val s]*
> *                (if (coll? val)*
> *                  (recur (cons (class val) ret) (first val))*
> *                  (cons (class val) ret))))))*
>
> which basically tries to give a list of  all the nested types of a value.
> If a value is a collection, then all its elements are of same type.
> so the above function when applied on .. let us say
>
> *(mclass #{'([1 2] [3 4])*
> *                  '([5 6] [7 8])})*
>
> the return value would be
>
> *[clojure.lang.PersistentHashSet clojure.lang.PersistentList
> clojure.lang.PersistentVector java.lang.Integer]*
>
> I would like to despatch based on some pattern of the return value of my
> dispatch function *mclass*
> *
> *
> For instance I would like to say something like when the second element of
> return value of the dispatch function is *PersistentList  call method1 and
> call method2 when say the first element is PersistentList and second
> element is say PersistentHashSet  and call method3 for some other pattern.
> Basically a general pattern matching .. So if only I could have a way of
> changing how the equality for the purpose of dispatching behaves .. it would
> be wonderful. We could either do this or we could change the dispatch-val to
> a dispatch-function with a single argument .. Or is there a way to already
> do this?*
>
> In short is it possible to do a generic pattern-matching during dispatch in
> multimethods?
>
> Sunil.
>
> --
> 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<clojure%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en

-- 
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