Difference between Symbols and Vars in Clojure

There’s a symbol + that you can talk about by quoting it: user=> ‘+ + user=> (class ‘+) clojure.lang.Symbol user=> (resolve ‘+) #’clojure.core/+ So it resolves to #’+, which is a Var: user=> (class #’+) clojure.lang.Var The Var references the function object: user=> (deref #’+) #<core$_PLUS_ clojure.core$_PLUS_@55a7b0bf> user=> @#’+ #<core$_PLUS_ clojure.core$_PLUS_@55a7b0bf> (The @ sign is … Read more

How to compare two functions for extensional equivalence, as in (λx.2*x) == (λx.x+x)?

It’s pretty well-known that general function equality is undecidable in general, so you’ll have to pick a subset of the problem that you’re interested in. You might consider some of these partial solutions: Presburger arithmetic is a decidable fragment of first-order logic + arithmetic. The universe package offers function equality tests for total functions with … Read more

Why such implementation of partial in clojure.core

Yes, it’s a performance optimization. I’ts not just about not calling concat – it’s about the fact that & in the argument list requires a collection to be created as well. The clojure core libraries tend to take performance seriously, under the assumption that the basic building blocks of the language will be present in … Read more

Function call in -> threading macro

The documentation for the -> and ->> macros state that the forms after the first parameter are wrapped into lists if they are not lists already. So the question is why does this not work for #() and (fn ..) forms? The reason is that both forms are in list form at the time the … Read more

Clojure: creating new instance from String class name

There are two good ways to do this. Which is best depends on the specific circumstance. The first is reflection: (clojure.lang.Reflector/invokeConstructor (resolve (symbol “Integer”)) (to-array [“16”])) That’s like calling (new Integer “16”) …include any other ctor arguments you need in the to-array vector. This is easy, but slower at runtime than using new with sufficient … Read more

How can I display the definition of a function in Clojure at the REPL?

An alternative to source (which should be available via clojure.repl/source when starting a REPL, as of 1.2.0. If you’re working with 1.1.0 or lower, source is in clojure.contrib.repl-utils.), for REPL use, instead of looking at functions defined in a .clj file: (defmacro defsource “Similar to clojure.core/defn, but saves the function’s definition in the var’s :source … Read more