Fork me on GitHub
#beginners
<
2016-10-22
>
eslachance05:10:07

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?

eslachance05:10:10

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 "[email protected]"]
 #object[dithcord.core$handle_hello 0x5f01fcc8 "[email protected]"]
 #object[dithcord.core$handle_dispatch 0x5cf15767 "[email protected]"]]

akiroz05:10:39

well that didn't work.

akiroz05:10:06

anyway, I was trying to show this:

(map #(% "msg") [#(str % "-1") #(str % "-2") #(str % "-3")])
=> ("msg-1" "msg-2" "msg-3")

akiroz05:10:39

@eslachance

eslachance05:10:35

Now try with the apply function though

eslachance05:10:12

Because I need to merge 2 things together, session and msg, so they're both arguments of the functions when I call them

eslachance05:10:09

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))))

eslachance05:10:24

waiiiiiit a second

eslachance05:10:41

just realized it's not apply that's complaining it's handle-ready

eslachance05:10:06

(map #(% session msg) (:internal-handlers @session)) looks pretty right about now... picard-facepalm

eslachance05:10:12

Well, progress I just get (nil nil nil)

eslachance05:10:28

thanks for the help @akiroz you made me realize at least a mistake of mine 😉

akiroz05:10:02

happy debugging~

eslachance06:10:23

Thanks 😁

eslachance13:10:28

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

eslachance13:10:35

Fixed with (map #(apply % [session msg]) (:internal-handlers @session))

eslachance13:10:44

Good night sleep never hurts anyone

neurogoo19:10:00

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.

neurogoo19:10:33

Anyone had similar problems? I doesn't seem to make sense that lein can find the file, but cider can not

pawel.kapala19:10:06

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!