On Sunday, February 22, 2015 at 12:52:04 PM UTC-5, Timur wrote:
>
> Hi everyone, 
>
> I have the following question regarding the defmultis of clojure: 
>
> (defmulti create-fact
>   (fn [item-vector] (do
>                       (print item-vector)
>                       (first item-vector))))
>
> (defmethod create-fact [:a] [item-vector]
>   (str "a"))
>
> (defmethod create-fact [[:a "safs"]] [item-vector]
>   (str "safs"))
>
>
> (mapv create-fact {:a "safs"})
>
>
> Dispatch function is not called in this case and return is "safs" so the 
> matching key is [[:a "safs"]]. I except it to be :a, why is that [[:a 
> "safs"]]?
>

Not sure why your print is not working, but it's otherwise behaving as 
expected. Treating a map as a seq produces a seq of map-entry objects, 
rather than alternating keys and values. (You can use mapcat instead of 
map/mapv to get alternating keys and values though.) So this is calling 
create-fact once, with a parameter of [:a "safs"] (actually a map-entry 
object, but prints like and can often be used as a two-element vector, and 
to some extent vice versa, for instance (into {} [[:a 1][:b 2]]) produces 
{:a 1 :b 2}).

-- 
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
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to