2

So I am trying to write the below in clojure (Assume all methods below return boolean)

def some_method(a, b)
  if (call_this_method() )
    then_call_this_method()
  else
    new_method()
end

What I got was this:

(defn some-method [a b]
  (if (call_this_method) 
   :then (then-call-this-method) 
   :else (new-method)))

I am pretty new to clojure so am not sure if this feels like the correct manner to solve this. Is there a different approach?

1

2 Answers 2

5

if basically takes 3 params, [condition what-to-run-if-true optional-run-if-false]

(defn some-method
    "My function does ..."
    [ a b ] 
    (if (call-this-method)
        (then-call-this-method)
        (new-method)))
Sign up to request clarification or add additional context in comments.

1 Comment

If you need several clauses in either then or else branch use (do (function1) (function2))
5

You can use if in clojure as well

(if test then-code else-code)

Or cond which is more like switch

(cond
    test-A run-if-A
    test-B run-if-B
    ...
    :else else-code)

And if you wanted to do something like

 if(foo) bar;

Then you would write

 (when foo bar)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.