Home > Enterprise >  Clojure: Conditionally call a function and rest of the other functions by default
Clojure: Conditionally call a function and rest of the other functions by default

Time:11-01

I want to execute a function conditionally and rest of the other functions by default irrespective of the first condition being true or false.

Ex: `

(defn- publish
  [txn publisher domain-slug template first-published-at]
  (if (= 2 2) (do (somefunc txn publisher)))
    (firstfunc txn publisher domain-slug first-published-at)
    (secondfunc txn publisher)
)

`

I want to execute all the three functions if true and execute the last two functions if false.

CodePudding user response:

when is intended to be used used for conditional side-effects. e.g.

(defn- publish
  [txn publisher domain-slug template first-published-at]
  (when (= 2 2) 
    (somefunc txn publisher))
  (firstfunc txn publisher domain-slug first-published-at)
  (secondfunc txn publisher))

CodePudding user response:

You can try

 (cond 
   (= 2 2) (some-fn arg1 arg2)
    :else  (firstfunc txn publisher domain-slug first-published-at)
           (secondfunc txn publisher))

Refer here https://clojuredocs.org/clojure.core/cond for syntax

  • Related