3

我从我找到的一个示例博客文章(但现在似乎无法检索)中无耻地提取了几乎所有这些代码,该文章演示了使用 Java 的套接字实现来维护一个简单的 IRC 连接。

在玩了一下这个例子时,我遇到了一个问题,我想加入一系列频道。我的第一个想法是在服务器 var 上存储一个通道向量,然后在匹配模式时对其进行解构。然而,这还没有奏效。如果我将地图更改为对 write 函数的裸调用,则一切正常。但是映射写入功能似乎不起作用,我不知道为什么。有趣的是,如果我将地图调用复制并粘贴到运行活动连接的 repl 中,它就可以工作......这是因为这发生在 cond 的范围内吗?裁判可能是罪魁祸首吗?我刚刚进入 Clojure,所以我仍然非常想弄清楚事情。

废话不多说,代码如下:

  (ns irc
    (:import (java.net Socket)
            (java.io PrintWriter InputStreamReader BufferedReader)))

  (def server {:server "irc.example.net"
               :port 6667
               :channels ["#a" "#b"]})
  (def client {:realname "Lambda Bot" :nick "lambdabot"})

  (declare conn-handler)


  (defn in [a b]
    (re-find (re-pattern a) b))


  (defn startswith [a b]
    (in (str "^" a) b))


  (defn connect [server]
    (let [socket (Socket. (:server server) (:port server))
          in (BufferedReader. (InputStreamReader. (.getInputStream socket)))
          out (PrintWriter. (.getOutputStream socket))
          conn (ref {:in in :out out})]
      (doto (Thread. #(conn-handler conn server)) (.start))
      conn))


  (defn write [conn msg]
    (println msg)
    (doto (:out @conn)
      (.println (str msg "\r"))
      (.flush)))


  (defn conn-handler [conn server]
    (while (nil? (:exit @conn))
      (let [msg (.readLine (:in @conn))]
        (println msg)
        (cond
          (startswith "ERROR :Closing Link:" msg)
          (dosync (alter conn merge {:exit true}))
          (in " 001 " msg)
          ;; this guy below doesn't seem to work at all
          (map #(write conn (str "JOIN " %)) (:channels server))
          ;; this guy on the other hand seems to work fine
          ;;(write conn (str "JOIN " (first (:channels server))))
          (startswith "PING" msg)
          (write conn (str "PONG " (re-find #":.*" msg)))))))


  (defn login [conn user]
    (write conn (str "NICK " (:nick user)))
    (write conn (str "USER " (:nick user) " 0 * :" (:realname user))))

要运行它,您可以简单地调用:(def irc(连接服务器))然后(登录 irc 客户端)。非常感谢任何帮助!谢谢。

4

1 回答 1

6

map是一种惰性操作,即直到需要时才执行。当从 repl 调用时, repl 会导致它评估,因为它需要打印返回的值。

您可以使用doall来强制执行惰性操作。

(doall (map ... ))

更好的是使用doseq而不是map. map用于将一组值转换为另一组值,doseq并对一组值执行操作以及您想要执行的操作。

(doseq [ch (:channels server)]
  (write conn (str "JOIN " ch)))
于 2012-11-12T03:41:47.040 回答