【发布时间】:2017-08-22 05:38:57
【问题描述】:
我正在编写一个函数来将 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
..但来自ircMessageToMap 的“IRCMapifying”结果事先出现在控制台上,表明它已被正确评估。
doall 试图强制在函数中间实现结果 - 它没有效果。
我应该如何重写这个stringToIRCMessage 函数以使r 变量可用?
【问题讨论】:
-
您还必须修复您的
(do (assoc ...) (assoc ...)),因为其中第一个无效。请记住,Clojure 值是不可变的,(g (f x))与(do (f x) (g x))非常不同。
标签: clojure