"未结合的" clojure函数中的变量

时间:2017-08-21 21:34:02

标签: clojure

我正在编写一个函数来将IRC RFC2813消息解析为它们的组成部分。这包括两个函数,一个用于通过正则表达式拆分消息,另一个用于修改返回以处理某些特殊情况。

(let [test-privmsg ":m@m.net PRIVMSG #mychannel :Hiya, buddy."])

(defn ircMessageToMap [arg]
  "Convert an IRC message to a map based on a regex"
  (println (str "IRCMapifying " arg))
  (zipmap [:raw :prefix :type :destination :message]
          (re-matches #"^(?:[:](\S+) )?(\S+)(?: (?!:)(.+?))?(?: [:](.+))?$"
                      arg
                      )
          )
  )

(defn stringToIRCMessage [arg]
   "Parses a string as an IRC protocol message, returning a map"
     (let [r (doall (ircMesgToMap arg))])
     (println (str "Back from the wizard with " r))
     (cond
       ;Reformat PING messages to work around regex shortcomings
       (= (get r :prefix) "PING") (do
                                    (assoc r :type (get r :prefix))
                                    (assoc r :prefix nil)
                                  )
       ;Other special cases here
       :else r)
  )

我遇到的问题是stringToIRCMessage函数似乎没有实现ircMesgToMap的返回值。如果我评估(stringToIRCMessage test-privmsg),则println语句会给我:

Back from the wizard with Unbound: #'irc1.core/r

..但是" IRCMapifying" ircMessageToMap的结果预先显示在控制台上,表明它已正确评估。

doall试图迫使结果在函数中间实现 - 它没有效果。

我应该如何重写此stringToIRCMessage函数以使r变量可用?

1 个答案:

答案 0 :(得分:2)

你的let陈述中的错误。

应该是这样的:

  (let [r (doall (ircMesgToMap arg)) ]
     (println (str "Back from the wizard with " r))
     (cond
       ;Reformat PING messages to work around regex shortcomings
       (= (get r :prefix) "PING") (do
                                    (assoc r :type (get r :prefix))
                                    (assoc r :prefix nil)
                                  )
       ;Other special cases here
       :else r))