Is there any way to store the result of a computation performed in a precondition so that it can be used in the actual function body.
This is awful (costly-computation runs twice):
(defn bar [x]
{:pre [(> (costly-computation x) 1337)]}
(costly-computation x))
I would like to do something along these lines. But this doesn't work.
(defn smartbar [x]
(let [res (costly-computation x)]
{:pre [(> res 1337)]}
res))
You have multiple ways to tackle this. For example you can compute (costly-computation x) outside of (bar) function and pass it as a parameter to (bar) function.
Or you can compute (costly-computation x) inside (bar) function and validate there, no {:pre} condition.
Or write a Macro to do exactly what you want!
Your smartbar isn't going to work because that isn't how defn macro is created to do.
Try this inside the REPL: