On Mon, Oct 13, 2008 at 11:14 PM, Stuart Halloway
<[EMAIL PROTECTED]> wrote:
>
> Hi Patrick,
>
> How about:
>
> (defmulti length (fn [x]
> (if (= :stateMachine (:class x))
> (:state x)
> (:class x))))
>
> (defmethod length :yardstick [x] 36)
> (defmethod length :walking [x] "short")
> (defmethod length :running [x] "long")
>
> user=> (length {:class :yardstick})
> 36
> user=> (length {:class :stateMachine :state :walking})
> "short"
> user=> (length {:class :stateMachine :state :running})
> "long"
>
> It would probably be better to have the fn return a vector so you
> don't have to worry about :state and :class values with colliding
> names, but that's the basic idea.
>
In general, if you have a conditional or other enumeration of things
in your dispatch method it is a warning sign that you might not be
getting the leverage out of multimethods - their prime reason to exist
is to provide open, extensible case logic.
Never say never, but in this case it's better to use two multimethods.
Rich
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---