This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-10-22
Channels
- # bangalore-clj (2)
- # beginners (22)
- # boot (8)
- # cider (1)
- # cljs-dev (10)
- # clojars (1)
- # clojure (5)
- # clojure-austin (14)
- # clojure-conj (1)
- # clojure-dusseldorf (6)
- # clojure-france (3)
- # clojure-russia (11)
- # clojure-spec (3)
- # clojure-uk (2)
- # clojurescript (27)
- # datomic (4)
- # hoplon (68)
- # leiningen (7)
- # off-topic (1)
- # om (68)
- # onyx (12)
- # other-languages (1)
- # re-frame (19)
- # vim (2)
Hmm. So... I'm trying to run this little thing here, which presumably would run the list of 3 functions and add msg
to it (the line with map
and apply
)
(def internal-handlers
[handle-ready
handle-hello
handle-dispatch])
(defn on-message [ws msg session]
(do
(prn (str "[OP][" (:op msg) "][EVENT][" (:t msg) "]"))
(map #(apply % msg) (:internal-handlers @session))
)
)
My 3 functions are defined properly, but when I actually run this, I get:
ArityException Wrong number of args (4) passed to: core/handle-ready clojure.lang.AFn.throwArity (AFn.java:429)
Obviously it's treating the list of 3 functions as if they were arguments to the (apply)
function, which I wouldn't expect. What would be the quick fix for that?And as further debugging, to confirm it's indeed a list of functions (stored in the session
which is an atom)
(pprint (:internal-handlers @session))
[#object[dithcord.core$handle_ready 0x3ed76f59 "dithcord.core$handle_ready@3ed76f59"]
#object[dithcord.core$handle_hello 0x5f01fcc8 "dithcord.core$handle_hello@5f01fcc8"]
#object[dithcord.core$handle_dispatch 0x5cf15767 "dithcord.core$handle_dispatch@5cf15767"]]
anyway, I was trying to show this:
(map #(% "msg") [#(str % "-1") #(str % "-2") #(str % "-3")])
=> ("msg-1" "msg-2" "msg-3")
Now try with the apply
function though
Because I need to merge 2 things together, session
and msg
, so they're both arguments of the functions when I call them
Example function in the internal-handlers:
(defn handle-ready [session msg]
(prn "handle-ready executed")
(when (and (= 0 (:op msg)) (= "READY" (:t msg)))
(swap! session assoc :session-id (-> msg :d :session_id))))
waiiiiiit a second
just realized it's not apply
that's complaining it's handle-ready
whooooops
(map #(% session msg) (:internal-handlers @session))
looks pretty right about now...
Well, progress I just get (nil nil nil)
thanks for the help @akiroz you made me realize at least a mistake of mine 😉
Thanks 😁
Ahhh in the end I really did have it wrong. The last arg of apply
is a list of args and in this case, so it took all 4 elements of msg
and applied them
Fixed with (map #(apply % [session msg]) (:internal-handlers @session))
Good night sleep never hurts anyone
I am trying to start project using Reagent. I added one simple Clojure file to the project. If I use "lein figwheel" in the project folder everything works fine and I can access the website it has made. But now I am trying to start it through Cider and for some reason it complains about not able to find the namespace of my file.
Anyone had similar problems? I doesn't seem to make sense that lein can find the file, but cider can not
Hi. I’m trying to use specs for the first time in my code (after reading this: http://clojure.org/guides/spec). How do you usually keep your specs? Inline with the functions that they spec or separated from production code, like tests? At first glance specs seem to have more of definition nature, so I’d keep ‘em with production code, but use in tests to exercise my functions with generated data. Thanks!