If-break in Clojure -
i trying figure out equivalent of java in clojure:
public int compute(int x) { if (x < 0) return 0; // continue computing return result; } is there idiomatic clojure "break" processing within function , return result?
no. there no short-circuit return statement (or break or goto ... ). return implicit.
a near equivalent in clojure example is
(defn test [x] (if (< x 0) 0 (let [result (comment compute result)] result))) but return result without naming it:
(defn test [x] (if (< x 0) 0 (comment compute result))) these run, comment evaluates nil.
by way, if construct 2 expressions (rather full three) returns nil if test fails.
(if (< 3 0) 0) ; nil so there return.
Comments
Post a Comment