Question

I'm messing around in Clojure specifically the Noir web framework and trying to generate a random grid of tiles.

This is probably pretty bad code but I'm learning! :D

(def tiles [:stairs :stone :monster]) 

(defpage "/" []
     (common/layout
       [:div {:class "level"}
        (repeatedly 10 [:div {:class "row"}
          (repeatedly 10
            [:div {:class (str "tile " (name (rand-nth tiles)))}])])]))

But this code is throwing a exception:

Wrong number of args (0) passed to: PersistentVector - (class clojure.lang.ArityException)
Was it helpful?

Solution

repeatedly takes a function not a body so you need to wrap the bodies in functions:

(repeatedly 10 (fn []
                 [:div
                 {:class "row"}
                 (repeatedly 10 (fn []
                                  [:div {:class (str "tile " (name (rand-nth tiles)))}]))]))

OTHER TIPS

Answer:

user=> (repeatedly 10 (fn [] [:div]))
([:div] [:div] [:div] [:div] [:div] [:div] [:div] [:div] [:div] [:div])


user=> (repeatedly 10 [:div])    
ArityException Wrong number of args (0) passed to: PersistentVector  clojure.lang.AFn.throwArity (AFn.java:437)


user=> (doc repeatedly)
-------------------------
clojure.core/repeatedly
([f] [n f])
  Takes a function of no args, presumably with side effects, and
  returns an infinite (or length n if supplied) lazy sequence of calls
  to it
nil
user=>
(def tiles [:stairs :stone :monster]) 

(defpage "/" []
     (common/layout
       [:div {:class "level"}
        (repeatedly 10 (fn []
                 [:div
                 {:class "row"}
                 (repeatedly 10 (fn []
                                  [:div {:class (str "tile " (name (rand-nth tiles)))}]))]))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top