Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure - To pass a function as a var 'vs' To call a function from within the other function?

I was just wondering if there are any advantages/disadvantages to choose one over the other, or if it is purely just a design choice for a project's scope/workflow?

Scenario 1 : Using var and eval

 (defn fun1 [x] (prn (* x x)))
 (defn fun2 [y] (#'fun1 y))
 (eval (fun2 2))
 4
 nil

Scenario 2 : Creating a sequential chaining of function calls

(defn fun1 [x] (prn (* x x)))
(defn fun3 [y] (fun1 y))
(fun3 2)
4
nil

I really appreciate your responses, Thanks in advance!

like image 247
Snehaa Ganesan Avatar asked Dec 05 '25 16:12

Snehaa Ganesan


1 Answers

In Scenario 1, I believe you meant to do this:

(defn fun1 [x] (prn (* x x)))
(defn fun2 [y] (#'fun1 y))
(fun2 2)

It is almost never necessary (nor desirable) to use eval

For more detail on the difference between these Scenario 1 & 2, please see this question.

like image 194
Alan Thompson Avatar answered Dec 08 '25 08:12

Alan Thompson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!