Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Well, I know of nothing built-in that you could use (other than <code>let</code>, which you don't want). But the beauty is that you could build it yourself.</p> <p>One idea would be to make a "new" <code>defn</code>, where a <code>:return</code> keyword to the left of an expression signifies returning the value from that, not the last expression. A macro will be appropriate here, because we'll be building up a modified <code>(defn ...)</code> expression, which requires unevaluated code.</p> <pre><code>(defmacro defn-return-middle [nm arg-vec &amp; body-exprs] `(defn ~nm ~arg-vec ~(if (some #{:return} body-exprs) (let [[before _ [to-be-returned &amp; after]] (partition-by #{:return} body-exprs)] `(let [ret# (do ~@before ~to-be-returned)] ~@after ret#)) body-exprs))) </code></pre> <p>This expands something like:</p> <pre><code>(defn-return-middle f [] a :return b c) </code></pre> <p>To something like:</p> <pre><code>(defn f [] (let [ret (do a b)] c ret)) </code></pre> <p>For example, you could now do:</p> <pre><code>(defn-return-middle blah [a] (+ 1 a) :return (+ 2 a) (println "does this work?")) </code></pre> <p>And now in a REPL:</p> <pre><code>user&gt; (blah 5) does this work? =&gt;7 </code></pre> <p>(Yay!)</p> <p>Or for your example, you would now do:</p> <pre><code>(defn-return-middle test1 [] (start-server) :return (run-pvt-and-expect "PVT-0") (stop-server)) </code></pre> <p>True, the macro uses <code>let</code>, but it works by automating the <code>let</code> expansion you would make if you were handwriting it each time. Meaning that now, when using this macro, you would no longer handwrite <code>let</code>. Also, this macro does not currently work on function definitions that declare multiple arities. But it wouldn't be too difficult to modify it to work for those as well.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload