0

I am learning about closures in Clojure and this function confused me:

(defn inc-maker
  "Create a custom incrementor"
  [inc-by]
  #(+ % inc-by))

(def inc3 (inc-maker 3))

(inc3 7)
; => 10

As a JavaScript developer I couldn't figure out how the argument 7 was getting passed in because in Javascript you would have to write it like this:

const incMaker = incByX => incByY => incByX + incByY;

const incThree = incMaker(3);

incThree(7);

So my question is, and not that I'm wanting to turn Clojure in to Javascript, but is there a way to name parameters in anonymous Clojure functions?

1 Answer 1

3

You could be a little more verbose about how you declare your anonymous function.

#(+ % inc-by)

is actually expanded to an equivalent form such as

(fn [n] (+ n inc-by))

If you use %1, %2 etc, the expanded function will have more parameters.

You can find some example on this documentation

Sign up to request clarification or add additional context in comments.

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.