multimethod noob question

112 views
Skip to first unread message

Dennis Haupt

unread,
Jun 22, 2013, 9:35:06 AM6/22/13
to clo...@googlegroups.com
hi,

i was taking a look at multimethods:
(defmulti fac int)
(defmethod fac 1 [_] 1)
(defmethod fac :default [n] (*' n (fac (dec n))))

this works

however, this also works:

(defmulti fac print)
(defmethod fac 1 [_] 1)
(defmethod fac :default [n] (*' n (fac (dec n))))

what exactly is "int" or "print" doing? it doesn't seem to have an effect?

Chris Bilson

unread,
Jun 22, 2013, 10:04:10 AM6/22/13
to clo...@googlegroups.com
Dennis Haupt <d.ha...@gmail.com> writes:

> i was taking a look at multimethods:
> (defmulti fac int)
> (defmethod fac 1 [_] 1)
> (defmethod fac :default [n] (*' n (fac (dec n))))
>
> this works
>
> however, this also works:
>
> (defmulti fac print)
> (defmethod fac 1 [_] 1)
> (defmethod fac :default [n] (*' n (fac (dec n))))

This definately does not work when I try it:

user> (defmulti fac print)
#<Var@6427854d: #<MultiFn clojure.lang.MultiFn@4bddef9a>>
nil
user> (defmethod fac 1 [_] 1)
#<MultiFn clojure.lang.MultiFn@4bddef9a>
nil
user> (fac 1)
IllegalArgumentException No method in multimethod 'fac' for dispatch value: null clojure.lang.MultiFn.getFn (MultiFn.java:160)
1

The second arg to defmulti's job is to decide which method to call. The
print function always produces nil, so you would need a defmethod for
dispatch value nil to use print as a dispatch function:

user> (defmethod fac nil [_] 2)
#<MultiFn clojure.lang.MultiFn@4bddef9a>
nil
user> (fac 1)
12
nil

Notice the "12": the "1" is from print and the "2" is the value the
method produced.

Are you trying to print the dispatch values so you can see them for
tracing or something? If so, you could try something like:

user> (defn inspect [& stuff]
(println "inspect: " stuff)
(first stuff))
#<Var@5b1413a8:
#<user$eval336$inspect__337 user$eval336$inspect__337@68903261>>
nil
user> (inspect 1)
inspect: (1)
1
nil
user> (defmulti fac2 inspect)
nil
nil
user> (defmethod fac2 1 [_] 1)
#<MultiFn clojure.lang.MultiFn@e2df60d>
nil
user> (fac2 1)
inspect: (1)
1
nil
user>


Dennis Haupt

unread,
Jun 22, 2013, 10:20:41 AM6/22/13
to clo...@googlegroups.com
i am not trying anything, i just want to figure out what happens. this is my output for the "print" version:
user=> (defmulti fac print)
(defmethod fac 1 [_] 1)
(defmethod fac :default [n] (*' n (fac (dec n))))
#'user/fac
#<MultiFn clojure.lang.MultiFn@1afb02d>
#<MultiFn clojure.lang.MultiFn@1afb02d>
user=> (fac 1)
10-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-....until stackoverflow


2013/6/22 Chris Bilson <cbi...@pobox.com>
--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@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+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Chris Bilson

unread,
Jun 22, 2013, 10:28:23 AM6/22/13
to clo...@googlegroups.com
Dennis Haupt <d.ha...@gmail.com> writes:

> i am not trying anything, i just want to figure out what happens. this is my output for the "print" version:
> user=> (defmulti fac print)
> (defmethod fac 1 [_] 1)
> (defmethod fac :default [n] (*' n (fac (dec n))))
> #'user/fac
> #<MultiFn clojure.lang.MultiFn@1afb02d>
> #<MultiFn clojure.lang.MultiFn@1afb02d>
> user=> (fac 1)
> 10-1-2-3-4-5-6-7-8-9-10-11-12-13-14-15-16-17-18-19-20-21-22-23-24-25-26-27-28-....until
> stackoverflow

Since print produces nil for a dispatch value, and nil is not 1, it
always calls your :default method.

Dennis Haupt

unread,
Jun 22, 2013, 10:30:38 AM6/22/13
to clo...@googlegroups.com
yes. all glory to the repl that makes me figure out the internals via experiments :D
is there a way to just pass the given value along?


2013/6/22 Chris Bilson <cbi...@pobox.com>

Dennis Haupt

unread,
Jun 22, 2013, 10:33:25 AM6/22/13
to clo...@googlegroups.com
(ns experiments.MultiMethod)
(defmulti fac identity)
(defmethod fac 1 [n] (print "n:" n" <- ") 1)
(defmethod fac :default [n] (*' n (fac (dec n))))
(print (fac 125))

if i am getting this right, defmethod fac <stuff> [<stuff2>] is the equivalent to a scala or haskell pattern match, where stuff is the "match" and stuff2 is the complete set of original arguments?

can you provide an example where stuff2 has more information than stuff? for fac, both are equal....

thx :D


2013/6/22 Dennis Haupt <d.ha...@gmail.com>

Chris Bilson

unread,
Jun 22, 2013, 10:44:43 AM6/22/13
to clo...@googlegroups.com
Dennis Haupt <d.ha...@gmail.com> writes:

> yes. all glory to the repl that makes me figure out the internals via experiments :D
> is there a way to just pass the given value along?

Yes, that's what I meant by my inspect method in my original reply. It
prints the value and passes it along:

(defn inspect [& args]
(println "inspect:" args)
(first args))

Dennis Haupt

unread,
Jun 22, 2013, 10:46:13 AM6/22/13
to clo...@googlegroups.com
"identity" :)


2013/6/22 Chris Bilson <cbi...@pobox.com>
Reply all
Reply to author
Forward
0 new messages