3

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!

Snehaa Ganesan
  • 509
  • 3
  • 10
  • 2
    Note `eval` is a function so `(eval (fun2 2))` calls `(fun2 2)` and then `eval` on the result i.e. `(eval 4)` so there's no reason to prefer that approach. Calling functions via the var directly is also unnecessary since symbols are evaluted to the value of the corresponding var. – Lee Apr 11 '19 at 16:23
  • @Lee got it. Thanks! – Snehaa Ganesan Apr 11 '19 at 16:32

1 Answers1

3

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.

Alan Thompson
  • 29,276
  • 6
  • 41
  • 48