Re: matching with wild-cards in clojure multi-methods

2010-09-10 Thread Meikel Brandmeyer
Hi,

On 9 Sep., 21:47, Daniel Werner daniel.d.wer...@googlemail.com
wrote:

 Could this be a bug?

No. Clojure does not enforce contracts in several places. Feed wrong
things in and get undefined behaviour back. Only the contract is
guaranteed. Everything else is an implementation detail and might
change at any moment.

Sincerely
Meikel

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


Re: matching with wild-cards in clojure multi-methods

2010-09-09 Thread Daniel Werner
On 9 September 2010 07:31, Meikel Brandmeyer m...@kotka.de wrote:
 derive works with non-qualified keywords, but the contract disallows
 that:

Apparently the contract given in the docstring is being enforced in
derive's 2-arg definition, but the must be namespaced parts of the
assertions are missing from the 3-arg definition (the one which takes
a custom hierarchy). This difference is quite unexpected. Could this
be a bug?

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


Re: matching with wild-cards in clojure multi-methods

2010-09-08 Thread Daniel Werner
On Sep 6, 4:43 pm, Meikel Brandmeyer m...@kotka.de wrote:
 You can use qualified keywords with an hierarchy.

 (def your-hierarchy
   (- (make-hierarchy)
     (derive ::hello ::anything)
     (derive ::world ::anything)
     (derive ::city ::anything)
     (derive ::us ::anything)))

Building your own hierarchy would make it safe to use unqualified
keywords as well -- if I am not mistaken?

(- (make-hierarchy)
  (derive :hello :anything)
  ...)

Daniel

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


Re: matching with wild-cards in clojure multi-methods

2010-09-08 Thread Meikel Brandmeyer
Hi,

On 8 Sep., 21:49, Daniel Werner daniel.d.wer...@googlemail.com
wrote:

 Building your own hierarchy would make it safe to use unqualified
 keywords as well -- if I am not mistaken?

 (- (make-hierarchy)
   (derive :hello :anything)
   ...)

derive works with non-qualified keywords, but the contract disallows
that:

user= (doc derive)
-
clojure.core/derive
([tag parent] [h tag parent])
  Establishes a parent/child relationship between parent and
  tag. Parent must be a *namespace-qualified* symbol or keyword and
  child can be either a *namespace-qualified* symbol or keyword or a
  class. h must be a hierarchy obtained from make-hierarchy, if not
  supplied defaults to, and modifies, the global hierarchy.

(*Emphasis* mine)

So: no. You cannot use non-qualified keywords. Even for private
hierarchies.

Sincerely
Meikel

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


Re: matching with wild-cards in clojure multi-methods

2010-09-06 Thread Sunil S Nandihalli
It looks like the matchure http://github.com/dcolthorp/matchure.git does
this kind of thing .. :) .. I guess somebody has already written a library
to do what I wanted ...

On Mon, Sep 6, 2010 at 9:44 AM, Sunil S Nandihalli 
sunil.nandiha...@gmail.com wrote:

 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.comwrote:


 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.comwrote:


 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.comclojure%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.comclojure%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 

Re: matching with wild-cards in clojure multi-methods

2010-09-06 Thread Sunil S Nandihalli
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.comwrote:


 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.comwrote:


 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.comclojure%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.comclojure%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

Re: matching with wild-cards in clojure multi-methods

2010-09-06 Thread Sunil S Nandihalli
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.comwrote:


 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.comclojure%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.comclojure%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

Re: matching with wild-cards in clojure multi-methods

2010-09-06 Thread Meikel Brandmeyer
Hi,

On 6 Sep., 06:14, Sunil S Nandihalli sunil.nandiha...@gmail.com
wrote:

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

You can use qualified keywords with an hierarchy.

(def your-hierarchy
  (- (make-hierarchy)
(derive ::hello ::anything)
(derive ::world ::anything)
(derive ::city ::anything)
(derive ::us ::anything)))

(defmulti foo
  your-dispatch-fn-here
  :hierarchy your-hierarchy)

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

Sincerely
Meikel

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


Re: matching with wild-cards in clojure multi-methods

2010-09-06 Thread Sunil S Nandihalli
thanks Meikel for the suggestion .. I never thought of that .. (or actually
didn't know about it)..
I think that helps..
Sunil.

On Mon, Sep 6, 2010 at 8:19 PM, Meikel Brandmeyer m...@kotka.de wrote:

 Hi,

 On 6 Sep., 16:43, Meikel Brandmeyer m...@kotka.de wrote:

  (defmulti foo
your-dispatch-fn-here
:hierarchy your-hierarchy)

 Woops. You should use an atom for the hierarchy or use the Var in the
 call above: #'your-hierarchy.

 An atom would allow later modification of the hierarchy.

 Sincerely
 Meikel

 --
 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.comclojure%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

matching with wild-cards in clojure multi-methods

2010-09-05 Thread Sunil S Nandihalli
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
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: matching with wild-cards in clojure multi-methods

2010-09-05 Thread Mark Rathwell
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.comclojure%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