Skip to content

Clojure Helpers

Justin Conklin edited this page Nov 11, 2017 · 2 revisions

The insn.clojure namespace provides helpers to create Clojure fns and vars. The fn body expression(s) of the original Clojure versions being instead a single expression suitable for being :emit-ed.

(require '[insn.clojure :as bc])

(bc/defn my-inc ^long [^long n]
  [[:lload 1]
   [:ldc2 1]
   [:ladd]
   [:lreturn]])

(map my-inc (range 3)) ;; => (1 2 3)

Since the argument list is adorned with the proper metadata, the above generates a fn that implements Clojure's clojure.lang.IFn$LL interface (indicating primitive support, taking and returning a long), and will not box the argument or return type when appropriate.

(set! *unchecked-math* :warn-on-boxed)

(inc (my-inc 42)) ;; => 44

Note that it helps to have some understanding of how Clojure compiles fns before using these. To learn more, I would suggest using the excellent no.disassemble library to disassemble some Clojure fns.

For additional examples, see the demo file from the test-suite.