Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing a function bound to a var in clojure

Tags:

clojure

So if I understand this correctly, when I write:

(defn foo [x] (+ (* (- x 3) 2) (- x 3)))

foo gets bound to:

(fn [x] (+ (* (- x 3) 2) (- x 3)))

How do I access the function from foo? My intention is to change something in the function and return a new function.

like image 910
Fernet Avatar asked Jun 10 '26 21:06

Fernet


1 Answers

If you want to use the function value stored in the Var foo, just write foo, for example:

(def foo2 (comp - foo))
(foo 4) ;;=> 3
(foo2 4) ;;=> -3

There is no special deref notation for Vars: just use their name and they get resolved to their bound value. Functions are not something you change, but you can build functions out of other ones, like above.

like image 185
Michiel Borkent Avatar answered Jun 12 '26 12:06

Michiel Borkent