Hi Mark,
 Let us say my dispatch function always returns a vector of 4 keywords. now
I want to write my methods in the following way..

(defmethod foo [_          :hello    _         _   ] (str "I'm method 1"))
(defmethod foo [:world   _         :us       _  ] (str "I'm method 2"))
(defmethod foo [:city       _         :us      _  ] (str "I'm method 3"))

and so on ..

where '_' would match with anything ...

but I just realized I could probably create a type of my own which overrides
the default '=' operator and make it behave like this .. do you think that
would work?

Sunil.



On Mon, Sep 6, 2010 at 9:36 AM, Mark Rathwell <mark.rathw...@gmail.com>wrote:

>
> Sorry, little slow today.  I'm not sure I'm completely following what you
> are trying to achieve.  What do you want your method prototypes to look
> like? (e.g. modify the below to be what you are envisioning):
>
> (defmulti foo mclass)
> (defmethod foo "1" [s] (str "I'm method 1"))
> (defmethod foo "2" [s] (str "I'm method 2"))
>
>
>
> On Sun, Sep 5, 2010 at 11:58 PM, Sunil S Nandihalli <
> sunil.nandiha...@gmail.com> wrote:
>
>> Thanks Mark for you reply. Well I realized we can do that .. but I feel it
>> would be more expressive and readable if I could do the wild-card like
>> pattern matching when I am defining the method.
>> Sunil.
>>
>>
>> On Mon, Sep 6, 2010 at 9:25 AM, Mark Rathwell <mark.rathw...@gmail.com>wrote:
>>
>>>
>>> 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<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