Re: auxiliary methods like :before and :after for multimethods?

2012-07-28 Thread Chas Emerick
On Jul 28, 2012, at 4:36 PM, Vinzent wrote:

> 
> robert.hooke works fine with multimethods: 
> 
> user=> (defmulti foo class) 
> nil 
> user=> (defmethod foo :default [x] (str x)) 
> # 
> user=> (require '[robert.hooke :refer (add-hook)]) 
> nil 
> user=> (add-hook #'foo (fn [f & [x]] (str "K: " (f x 
> (#) 
> user=> (foo 42) 
> "K: 42" 
> 
> foo is a plain function now.

Oh, right, good point.  I've generally used hooks to modify others' 
multimethods, and so I suppose I've been lucky to always apply the hook after 
all methods had been registered.

- Chas

-- 
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: auxiliary methods like :before and :after for multimethods?

2012-07-28 Thread Vinzent


> robert.hooke works fine with multimethods: 
>
> user=> (defmulti foo class) 
> nil 
> user=> (defmethod foo :default [x] (str x)) 
> # 
> user=> (require '[robert.hooke :refer (add-hook)]) 
> nil 
> user=> (add-hook #'foo (fn [f & [x]] (str "K: " (f x 
> (#) 
> user=> (foo 42) 
> "K: 42" 
>

foo is a plain function now.

-- 
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: auxiliary methods like :before and :after for multimethods?

2012-07-28 Thread Chas Emerick
On Jul 28, 2012, at 7:02 AM, Ben Smith-Mannschott wrote:

> On Fri, Jul 27, 2012 at 9:06 PM, Vinzent  wrote:
>> robert-hooke actualy doesn't work with multimethods afaik. You can try my
>> new library (https://github.com/dnaumov/hooks), but it's alpha (no docs yet,
>> sorry).
> 
> (defmulti foo* (fn [args] ...) ...)
> (defmethod foo* :x [args]...)
> (defmethod foo* :y [args] ...)
> 
> (defn foo [args]
>  (foo* args))
> 
> Only foo calls foo*. Everyone else calls foo. Apply hooks to foo.
> 
> http://en.wikipedia.org/wiki/Fundamental_theorem_of_software_engineering
> 
> ;-)
> 
> // Ben

robert.hooke works fine with multimethods:

user=> (defmulti foo class)
nil
user=> (defmethod foo :default [x] (str x))
#
user=> (require '[robert.hooke :refer (add-hook)])
nil
user=> (add-hook #'foo (fn [f & [x]] (str "K: " (f x
(#)
user=> (foo 42)
"K: 42"

More interesting still would be the ability to add hooks to particular methods. 
 `defmethod` doesn't define a new var, so that's not generally possible, but 
you can work around it by defining functions and tying them to multimethods in 
separate operations:

user=> (defmulti twice class)
#'user/twice
user=> (defn twice-n [n] (* n n))
#'user/twice-n
user=> (defn twice-s [s] (str s s))
#'user/twice-s
user=> (.addMethod twice Number #'twice-n)
#
user=> (.addMethod twice String #'twice-s)
#
user=> (twice 5)
25
user=> (twice "hi")
"hihi"
user=> (add-hook #'twice-n (fn [f & [n]] (f (dec n 
(#)
user=> (twice 5)
16

These sorts of situations makes me want for an add-method to go along with 
remove-method and get-method, just to avoid the .addMethod interop form.

- Chas

-- 
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: auxiliary methods like :before and :after for multimethods?

2012-07-28 Thread Ben Smith-Mannschott
On Fri, Jul 27, 2012 at 9:06 PM, Vinzent  wrote:
> robert-hooke actualy doesn't work with multimethods afaik. You can try my
> new library (https://github.com/dnaumov/hooks), but it's alpha (no docs yet,
> sorry).

(defmulti foo* (fn [args] ...) ...)
(defmethod foo* :x [args]...)
(defmethod foo* :y [args] ...)

(defn foo [args]
  (foo* args))

Only foo calls foo*. Everyone else calls foo. Apply hooks to foo.

http://en.wikipedia.org/wiki/Fundamental_theorem_of_software_engineering

;-)

// Ben

> Any suggestions about API is welcome.

-- 
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: auxiliary methods like :before and :after for multimethods?

2012-07-28 Thread Vinzent
Can you elaborate more about how the system should behave?

Also, I don't think it's correct to say that "you own the target function" 
here, since hook applies to the whole defmulti, not to some concrete 
defmethod, so I don't see any difference between hooks for functions and 
multimethods in this case.

суббота, 28 июля 2012 г., 9:37:57 UTC+6 пользователь George Oliver написал:
>
>
>
> On Friday, July 27, 2012 12:06:33 PM UTC-7, Vinzent wrote:
>>
>> robert-hooke actualy doesn't work with multimethods afaik. You can try my 
>> new library (https://github.com/dnaumov/hooks), but it's alpha (no docs 
>> yet, sorry).
>
>
> Yes, from the robert-hooke readme, "Adding hooks to a defmulti is 
> discouraged as it will make it impossible to add further methods. Hooks are 
> meant to extend functions you don't control; if you own the target function 
> there are obviously better ways to change its behaviour.". 
>
> What got me thinking about :before and :after was the question of how to 
> add a lightweight rules system to an application. Do you think hooks are 
> appropriate here?
>

-- 
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: auxiliary methods like :before and :after for multimethods?

2012-07-27 Thread George Oliver


On Friday, July 27, 2012 12:06:33 PM UTC-7, Vinzent wrote:
>
> robert-hooke actualy doesn't work with multimethods afaik. You can try my 
> new library (https://github.com/dnaumov/hooks), but it's alpha (no docs 
> yet, sorry).


Yes, from the robert-hooke readme, "Adding hooks to a defmulti is 
discouraged as it will make it impossible to add further methods. Hooks are 
meant to extend functions you don't control; if you own the target function 
there are obviously better ways to change its behaviour.". 

What got me thinking about :before and :after was the question of how to 
add a lightweight rules system to an application. Do you think hooks are 
appropriate here?

-- 
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: auxiliary methods like :before and :after for multimethods?

2012-07-27 Thread Vinzent
robert-hooke actualy doesn't work with multimethods afaik. You can try my 
new library (https://github.com/dnaumov/hooks), but it's alpha (no docs 
yet, sorry).

Any suggestions about API is welcome.

пятница, 27 июля 2012 г., 3:15:44 UTC+6 пользователь George Oliver написал:
>
> hi, I'm wondering if anyone has extended multimethods with auxiliary 
> methods like CL-style :before and :after, and if not what a suitable 
> substitute might be. 
>
>
> thanks, George
>

-- 
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: auxiliary methods like :before and :after for multimethods?

2012-07-26 Thread Kevin Downey
https://github.com/technomancy/robert-hooke/

On Thu, Jul 26, 2012 at 2:15 PM, George Oliver  wrote:
> hi, I'm wondering if anyone has extended multimethods with auxiliary methods
> like CL-style :before and :after, and if not what a suitable substitute
> might be.
>
>
> thanks, George
>
> --
> 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



-- 
And what is good, Phaedrus,
And what is not good—
Need we ask anyone to tell us these things?

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


auxiliary methods like :before and :after for multimethods?

2012-07-26 Thread George Oliver
hi, I'm wondering if anyone has extended multimethods with auxiliary 
methods like CL-style :before and :after, and if not what a suitable 
substitute might be. 


thanks, George

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