Subject: Re: Multimethods dispatch value order





On Wednesday, January 11, 2017 at 4:23:28 PM UTC-6, david swift wrote:Been working on a tiny snippet of code related to multimethods (something I've never quite wrapped my head around) and when it comes to the dispatching of the function I'm confused by the order in which I've to pass the arguments in as it seems to go against all common sense.
To show what I am trying to do (to begin with); ignoring name conflictions. Without implementing algorithm details I supply a simple string for ease.(defmulti transform (fn [language data] language))

(defmethod transform :html
   [_ data]
   (str data " will be transformed into html")))
^ extra ) at the end there fyi 

(defmethod transform :java-fx
   [_ data]
   (str data " will be transformed into javafx"))
What I would expect from this outcome would be to do the following on dispatch(transform :html "some data") => "some data will be transformed into html"
This is what I see and that looks right to me. I suspect you somehow have old method impls in your repl state. You should try with a fresh repl. 
But instead the output I actually get ends up being "some data" which I assume to be the default for mulitmethods to return the data value. Though if I were to instead to change the order of the arguments like so.
(transform "some data" :html) => "some data will be transformed into html"The expect outcome I wanted but not with the "correct" argument order I was expecting, and to also note this (for me) prevents me from having more than one argument and dispatch case.
I get 
IllegalArgumentException No method in multimethod 'transform' for dispatch value: some data  clojure.lang.MultiFn.getFn (MultiFn.java:156)


I know that I can do this approach
(transform {:language :html} "some data")and it would work in the correct order but it...

's extra data I'm looking to avoid for the sake of correct argument order
Ideally I would of like the code to look something like below(defmulti transform :language)

(defmethod transform :html
   [data]
   (str data " will be transformed into html"))

(defmethod transform :java-fx
   [data]
   (str data " will be transformed into javafx"))
I'd be grateful if someone could give me some simple examples to go by that work by keyword dispatch that isn't foobaz, thank you :)

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@xxxxxxxxxxxxxxxx
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+unsubscribe@xxxxxxxxxxxxxxxx
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 unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscribe@xxxxxxxxxxxxxxxx.
For more options, visit https://groups.google.com/d/optout.
...



Programming list archiving by: Enterprise Git Hosting