On Jan 20, 12:13 am, Mark Fredrickson <mark.m.fredrick...@gmail.com>
wrote:
> On the subject of per defmulti hierarchies:
>
> The recent CLJOS thread got me thinking about this problem again.
> While allowing per-multimethod hierarchies solves a large number
> problems, I think it might be lacking for a class of needs. I think I
> have solution that can provide for multiple hierarchies and meet this
> larger class to which I allude. Allow me to provide an illustration.
> Say I have a step-wise mathematical function. From less than or equal
> to zero, return zero. From more than zero to less than equal to five,
> return the value. Above 5, square the value.
>
> I could implement that as multimethod:
>
> (defmulti stepwise (fn [n] (cond (>= 0 n) :zero (and (> n 0) (< n
> 5)) :zero-five :else :above-five)))
> (defmethod stepwise :zero [_] 0)
> (defmethod stepwise :zero-five [x] x)
> (defmethod stepwise :above-five [x] (* x x))
>
> I've left an intentional programming error in to show how easy it is
> to make an error with such (cond ...) based dispatch. Yuck.
>
> But there is a better way. What if I could supply my own function to
> decide which key matched the multimethod (not what the key is for the
> value, but how the key maps to a multimethod). Currently all we have
> is isa?, but if this were only the default? My example might look
> something like:
>
> (defmulti stepwise identity <=)
> (defmethod stepwise 0 [_] 0)
> (defmethod stepwise 5 [n] n)
> (defmethod stepwise :default [n] (* n n))
> (prefer-method stepwise 0 5) ; since -1 < 0 and -1 < 5
>
> I find this much cleaner, and the subtle error above was been
> eliminated. And if I want to add another case, its not too hard
> (though the number of prefer-methods might increase exponentially).
> This is a rather trivial case, but I think it makes the need clear:
> per-multi comparison functions.
>
> For multiple hierarchies, this would be a common idiom:
> (defmulti mymulti dispatch-function (fn [object-key method-key] (isa?
> *my-hiearchy* object-key method-key))
>
> A user could use a ref or atom as needed, no special casing in the
> Clojure code.
>
> What do people think?
This seems to me to be a half step towards predicate dispatch. With
the a la carte hierarchies freeing us from type-based inheritance, it
might be worth someone revisiting Chambers/Chen style predicate
dispatch.
Rich
--~--~---------~--~----~------------~-------~--~----~
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
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
-~----------~----~----~----~------~----~------~--~---