Reputation: 343
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)
Upvotes: 2
Views: 246
Reputation: 21
(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)))}]))]))
Upvotes: 1
Reputation: 20934
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)))}]))]))
Upvotes: 5
Reputation: 1412
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=>
Upvotes: 4