Skip to content

Instantly share code, notes, and snippets.

@waffletower
Last active April 10, 2025 23:09
Show Gist options
  • Save waffletower/2577a19da18f751b10298235005e601e to your computer and use it in GitHub Desktop.
Save waffletower/2577a19da18f751b10298235005e601e to your computer and use it in GitHub Desktop.
Bind metadata to a Clojure function without a var via defrecord
Via clojure.core, metadata associations are only available to vars.
This is a method to associate metadata with lambda functions,
perhaps purposely exposing data a function closes upon, as encapsulation
is the *only* supported behavior for closure data.
(defrecord MetaFn
[md f]
clojure.lang.IFn
(applyTo [this args]
(apply (:f this) args))
(invoke [this]
((:f this)))
(invoke [this x1]
((:f this) x1))
(invoke [this x1 x2]
((:f this) x1 x2))
(invoke [this x1 x2 x3]
((:f this) x1 x2 x3))
(invoke [this x1 x2 x3 x4]
((:f this) x1 x2 x3 x4)))
(defn db-env-init
"returns an envelope function chain, closing on the envelope, and adds a decibel to linear post-processor function.
adds envelope points as metadata to returned function."
[envelope-fn]
(->MetaFn
(-> (envelope-fn 0)
:points)
(fn [n]
(comp
db->linear
(-> (envelope-fn n)
prepare
realizer)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment