Take the 2-minute tour ×

This question is related to one I asked recently.

If I rewrite (fn [n] (fn [x] (apply * (repeat n x)))) as

(defn power [n] (fn [x] (apply * (repeat n x))))`

it works just fine when used like this

((power 2) 16)

I can substitute 2 with another power, but I'd like to make a function just for squares, cubed, and so on. What is the best way to do that? I've been fiddling with it in the REPL, but no luck so far.

share|improve this question
I didn't get your question. Do you want to create collection of functions: square, cube, etc? –  Nikita Beloglazov Oct 2 '11 at 15:06
Yes. I'd rather take the (power 2) syntax and replace it with power2, power3 (cubed), and so on. Of course there is a math library, and in production code, I wouldn't do this, but I'm trying to map the original function into how it would receive its arguments, and trying this out in the REPL helped immensely. –  octopusgrabbus Oct 2 '11 at 15:14

2 Answers 2

up vote 7 down vote accepted

Using a macro for this goes entirely around his question, which was "I have a function that generates closures, how do I give those closures names?" The simple solution is:

(letfn [(power [n]
          (fn [x]
            (apply * (repeat n x))))]
  (def square (power 2))
  (def cube (power 3)))

If you really truly hate repeating def and power a few times, then and only then is it time to get macros involved. But the amount of effort you'll spend on even the simplest macro will be wasted unless you're defining functions up to at least the tenth power, compared to the simplicity of doing it with functions.

share|improve this answer
Yes, but his question seemed wrongly expressed I wasn't sure it was that simple. The "and so on" part was telling me that he was searching for more than what he actually asked for? I upvoted you for preciseness. –  Nicolas Buduroi Oct 3 '11 at 0:38
The (def square (power 2)) was what I was looking for, but having a macro answer was also helpful. Personally, I'd use the (power 2) as is, but was wondering how to shorten it. –  octopusgrabbus Oct 4 '11 at 13:33

Not quite sure if this is what you're searching for, but macro templates might be it. Here's how I would write your code:

(use 'clojure.template)

(do-template [name n]
  (defn name [x] (apply * (repeat n x)))
  square 2
  cube   3)

user> (cube 3)
;=> 27

For more complex type of similar tasks, you could write a macro that wrap do-template to perform some transformation on its arguments, e.g.:

(defmacro def-powers-of [& ns]
  (let [->name #(->> % (str "power") symbol)]
    `(do-template [~'name ~'n]
       (defn ~'name [~'x] (apply * (repeat ~'n ~'x)))
       ~@(->> ns
              (map #(vector (->name %) %))

(def-powers-of 2 3 4 5)

user> (power3 3)
;=> 27

user> (power5 3)
;=> 243

P.S.: That macro might look awkward if you're just starting with Clojure though, don't give up because of it! ;-)

share|improve this answer
Thanks. This is a great answer. A lot my problem learning Clojure is getting my eyes wrapped around the syntax. The more I fiddle with little dabs that would not be necessarily used in production, the better I understand things. –  octopusgrabbus Oct 2 '11 at 18:30

Your Answer


