Idiomatic clojure map lookup by keyword

(:color my-car) is fairly standard. There are a few reasons for this, and I won’t go into all of them. But here’s an example. Because :color is a constant, and my-car is not, hotspot can completely inline the dynamic dispatch of color.invoke(m), which it can’t do with m.invoke(color) (in some java pseudo-code). That gets even … Read more

Clojure: reduce vs. apply

reduce and apply are of course only equivalent (in terms of the ultimate result returned) for associative functions which need to see all their arguments in the variable-arity case. When they are result-wise equivalent, I’d say that apply is always perfectly idiomatic, while reduce is equivalent — and might shave off a fraction of a … Read more

How to handle java variable length arguments in clojure?

Since Java varargs are actually arrays, you can call vararg functions in Clojure by passing an array. You could convert a Clojure seq (maybe by using Clojure’s variety of variable argument functions) into an array: (TestClass/aStaticFunction (into-array Integer [(int 1),(int 2)])) or (defn a-static-function-wrapper [& args] (TestClass/aStaticFunction (into-array Integer args)) Or make an array and … Read more

Why exactly is eval evil?

There are several reasons why one should not use EVAL. The main reason for beginners is: you don’t need it. Example (assuming Common Lisp): EVALuate an expression with different operators: (let ((ops ‘(+ *))) (dolist (op ops) (print (eval (list op 1 2 3))))) That’s better written as: (let ((ops ‘(+ *))) (dolist (op ops) … Read more