I believe it stands for "member function". From the doc string:
"Expands into code that creates a fn that expects to be passed an
object and any args and calls the named instance method on the
object passing the args. Use when you want to treat a Java method as
a first-class fn."
.add is macroexpanded into the more general dot form, as shown in the
memfn expansion.
user=> (read-string "(.foo bar)")
(.foo bar)
user=> (macroexpand-1 *1)
(. bar foo)
http://clojure.org/java_interop#dot
> (doc .add) throws an exception rather than recognizing it as a
> special form
doc looks up the docstring of a var. If the argument isn't a var, it
will throw an exception.
If you break your contract with Clojure and make a var with a dot-name
anyway, doc will work:
user=> (def .add 5)
#'user/.add
user=> (doc .add)
-------------------------
user/.add
nil
nil
nil
> or similarly, though you also get strange errors if you try to
> use .add as a variable name.)
"Symbols beginning or ending with '.' are reserved by Clojure.".
You don't get errors if you use .add as a var or the name of a symbol
(see above), though you're disobeying the docs.
You do get an error if you use it as the name of a local binding symbol:
user=> (let [.foo 5])
java.lang.ClassFormatError: Illegal field name ".foo" in class user
$eval__10 (NO_SOURCE_FILE:0)
When you think about what this is actually doing in terms of Java
classes, this message makes sense.
-R
.add is macroexpanded into the more general dot form, as shown in the
> user=> (macroexpand-1 '(memfn add x y))
> (clojure.core/fn [target__4193__auto__ x y] (. target__4193__auto__
> (add x y)))
>
> That is, basically (fn [object x y] (.add object x y)).
memfn expansion.
user=> (read-string "(.foo bar)")
(.foo bar)
user=> (macroexpand-1 *1)
(. bar foo)
Hi,
Don't think to complicated.
On Aug 3, 10:10 pm, John Harrop <jharrop...@gmail.com> wrote:
> (defn and-ns
> "A non-short-circuiting \"and\" usable in reduce etc."
> ([] true)
> ([a] a)
> ([a b] (and a b))
> ([a b & more] (reduce and-ns (and a b) more)))
(reduce #(and %1 %2) coll)
This should do the trick, no? It's still non-short-circuiting, though.
I think memfn is some relict like .., which pre-dates some
of the nice additions to clojure.
#(.foo %1 %2 fixed-arg %3)
Yes, memfn preceded #(), the latter now being preferred.
Rich