I was watching David Nolan's talk about predicate
dispatch<http://blip.tv/clojure/david-nolen-predicate-dispatch-5953889>,
and he articulated something that I've found frustrating about multimethods
- the set of implementations is open, but the dispatch function is closed.
That means that if your dispatch value can't be computed in the same way
for all your data, you can't use multimethods.

Predicate dispatch allows implementations to be registered with a test that
decides whether or not they are applicable, so it can do things that
multimethods cannot. For example, predicate dispatch would allow you to
write an open function that categorises integers as "prime", "perfect" or
some other category without baking them all into the dispatch function.

In his talk, David speaks about the problem of overlap. 2 is both "prime"
and "even", for example. That got me thinking whether overlap is a bug or a
feature.

I've hacked together a simple plural dispatch
system<https://gist.github.com/3634871>,
which allows the specification of a custom resolution function that can
decide whether to use one or all of the registered implementations. You can
use this system to implement both multimethods and predicate dispatch.

What practical reason would you have for doing this? Random dispatch! Can't
decide which implementation to use? Now you don't have to!

(defplural random (fn [implementations args] (-> implementations rand-nth
(apply args))))
(defimplementation random #(+ % 100))
(defimplementation random #(- % 100))
(random 1)

It could also be useful for gathering a set of all possible results,
quasi-AOP extension points, broadcasting events etc.

Cheers,

Chris

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