Does Clojure repeatedly initiate new class(object) for every function call? - clojure

What is the low-level operation underneath of the function call?
What kind of Java code will be generated when we call a function from Clojure?
I would like to know the details of it so I can write more performance focused code.

In clojure, anything invoked as a function must implement the clojure.lang.IFn interface. This interface includes a overloaded method called invoke. Functions are first-class in Clojure, so when you say (def square (fn [x] (* x x))), square is assigned to the instance of a class that implements IFn.
So to sum up: invoking a function in Clojure incurs the cost of calling a method on an object.

Clojure functions implement the IFn interface, which provides the invoke() call with numerous different signatures. Unless you're passing more than 20 arguments, in which case a variadic signature is in use, it's just as fast as any other method invocation in the JVM.

Related

How to write a java varg function in clojure?

In Java, a varg function can be written like this:
public static void foo(int ... a)
{
// method body
}
It gets called in Java like this:
<OBJ>.foo(1, 2, 3);
and it gets called in Clojure like this:
(<OBJ>/foo (int-array [1 2 3])
Is it possible to write foo in Clojure so that it gets called in Java as a varg function?
Unfortunately, there is no option to make Java-style varargs methods (that is, methods that accept arrays and have the VARARGS (0x80) bit set to true).
Instead, you can either make a function that accepts an array (and write a varargs wrapper around it in Java, if you need varargs) or make a Clojure's [&rest] function and .invoke() it with varargs.

Clojure - more idiomatic to return a closure, or partially apply the function?

I'm using an external library, and passing it a function that I write. Something like this, for example:
(ext-func my-func) ...
my-func needs to be given some data to do computation. The way I see it, I have two basic choices:
1) Write my-func in such a way that it accepts my data, and returns a function, which will then have the data bound to it via closure when the external library calls it. For example:
(defn my-func
[mydata]
(fn []
(... access to mydata via closure ... )))
(ext-func (my-func somedata))
2) Do not return a function from my-func, but bind data to it when I pass it to ext-func:
(defn my-func
[mydata]
(... evaluate, use mydata, etc.))
(ext-func (partial my-func somedata))
I suppose which one to use could be answered by how I intend to use the function otherwise. If I'm going to be using it other places, I may prefer not to return a function, for example. But, all other things being equal...
...which of these is the more idiomatic approach?
partial is just sugar to create the anonymous function. Check out it's source. So, effectively they're equivalent. Take your pick. Neither is more idiomatic, just a matter of personal preference.

How do I tag a Clojure function so that I could recognize it with Java reflection

I need to somehow tag certain Clojure functions as "special" so that Java code could recognize them as such using reflection. I've tried to add an annotation to a function, but apparently that's not supported. I've tried to reify an interface extending IFn (so that the Java code could recognize the function object), but that's no good because Clojure doesn't directly use the reified method as the code implementing invoke, but rather an indirect call to an Afunction object that's actually implementing the method (I need to tag the actual invoke method with the actual function code).
Any ideas?
EDIT: even tagging in a way that could be accessed with the ASM library (rather than regular reflection) would be fine, but I need to somehow tag the actual AFunction object or the invoke method. Also, I can't access the actual AFunction object -- I need the tag to be visible on the class.
You can use clojure meta-data feature which allows meta data (a map) to be attached to any object that implements IMeta interface (which turns out to be every object as IObj extends IMeta and every object extend IObj)
Now there are 2 options.
1) You can attach the meta data to a var (the var points to the actual IFn object)
(defn hello {:name "hello"} [] 10)
and on Java side you get hold of the var hello and use IMeta methods to get the meta data and detect if your specific meta data is there or not.
The problem with this may be that your Java code access/handle IFn objects directly rather than their vars (ex: Anonymous functions), to solve this try the 2nd option.
2) Attach the meta data to the function object itself:
(def hello (with-meta (fn [] 10) {:name "hello"}))
You cannot use defn as that attach meta data to the var. The above sample code attach the meta data to the function object itself. On Java side, typecase the function object to IMeta and do the check.
The above code can be made a bit more defn likish with a help of a macro which is left as an exercise :)
It turns out that if you enclose the function body with a let statement containing a local definition, that variable name will appear in the method's local table in the class file. A bit cumbersome, though. Still looking for a better way.

Clojure record constructors not first class?

Apparently, you can't call apply with a record constructor:
(defrecord Foo. [id field])
(apply Foo. my-list)
fails at read time because it is not expecting Foo. in that place.
The only obvious workaround I could think of was to add a factory function:
(make-foo [id field] (Foo. id field))
which can be apply'ed of course.
Am I missing anything? I'd expect this from C#/Java but just thought it was a bit disappointing in Clojure...
Circling back on this post-1.3....
In Clojure 1.3, defrecord creates two generated constructor functions. Given:
(defrecord Person [first last])
this will create a positional constructor function ->Person:
(->Person "alex" "miller")
and a map constructor function map->Person:
(map->Person {:first "string"})
Because this is a map, all keys are optional and take on a nil value in the constructed object.
You should require/use these functions from the ns where you declare the record, but you do not need to import the record class as you would when using the Java class constructor.
More details:
http://dev.clojure.org/display/design/defrecord+improvements
http://groups.google.com/group/clojure/browse_thread/thread/ce22faf3657ca00a/beb75e61ae0d3f53
Foo. is a Java class constructor so it has typical Java interop constraints with how you call it. Creating a constructor function is a common solution (it also means you don't have to import the Foo when in a different namespace).
The problem is known and there is lots of talk about it on the Clojure mailing list. More support will probably be added in future Clojure versions.
For now you have to use your own functions or use https://github.com/david-mcneil/defrecord2 which supports some features like:
print in an eval'able form
provide clojure function as constructor
accept named parameters (maps) in constructor
participate in pre/post walk multi-method

How does clojure's defrecord method name resolution work?

After defining a record and the interfaces it implements, I can call its methods either by its name or using the java interop way using the dot operator.
user=> (defprotocol Eat (eat [this]))
Eat
user=> (defrecord animal [name] Eat (eat [this] "eating"))
user.animal
user=> (eat (animal. "bob"))
"eating"
user=> (.eat (animal. "bob"))
"eating"
user=>
Under the surface, what is going on there? Are there new clojure functions being defined? What happens when there are functions you defined that share the same name (is this possible?), how are these ambiguities resolved?
Also, is it possible to "import" java methods for other java objects so that you do not need the . operator so that behavior is like above? (For the purpose, for example, of unifying the user interface)
When you define a protocol, each of its methods are created as functions in your current namespaces. It follows that you can't have two protocols defining the same function in the same namespace. It also means that you can have them in separate namespaces and that a given type can extend both[1] of them without any nameclash because they are namespaced (in opposition to Java where a single class can't implement two interfaces with homonymous methods).
From a user perspective, protocol methods are no different from plain old non-polymorphic functions.
The fact that you can call a protocol method using interop is an implementation detail. The reason for that is that for each protocol, the Clojure compiler creates a corresponding backing interface. Later on when you define a new type with inline protocol extensions, then this type will implement these protocols' backing interfaces.
Consequently you can't use the interop form on an object for which the extension hasn't been provided inline:
(defrecord VacuumCleaner [brand model]
(extend-protocol Eat
VacuumCleaner
(eat [this] "eating legos and socks"))
(.eat (VaacumCleaner. "Dyson" "DC-20"))
; method not found exception
The compiler has special support for protocol functions so they are compiled as an instance check followed by a virtual method call, so when applicable (eat ...) will be as fast as (.eat ...).
To reply to "can one import java methods", you can wrap them in regular fns:
(def callme #(.callme %1 %2 %3))
(obviously you may need to add other arities to account for overloads and type hints to remove reflection)
[1] however you can't extend both inline (at least one of them must be in a extend-* form), because of an implementation limitation