如何创建从字符串而不是文件或 url 读取的输入流

发布于 2024-10-15 10:22:52 字数 44 浏览 3 评论 0原文

我想将 *in* 绑定到从字符串而不是“真实”输入流读取的流。我该怎么做?

I want to bind *in* to stream that's reading from a string instead of the "real" input stream. How do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的奇迹 2024-10-22 10:22:52

查看 with-in-str

http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/with-in-str

ClojureDocs 有一个使用示例:

;; Given you have a function that will read from *in*
(defn prompt [question]
  (println question)
  (read-line))

user=> (prompt "How old are you?")
How old are you?
34                   ; <== This is what you enter
"34"                 ; <== This is returned by the function

;; You can now simulate entering your age at the prompt by using with-in-str

user=> (with-in-str "34" (prompt "How old are you?"))
How old are you?
"34"                 ; <== The function now returns immediately 

Check out with-in-str:

http://clojure.github.com/clojure/clojure.core-api.html#clojure.core/with-in-str

ClojureDocs has an example of its use:

;; Given you have a function that will read from *in*
(defn prompt [question]
  (println question)
  (read-line))

user=> (prompt "How old are you?")
How old are you?
34                   ; <== This is what you enter
"34"                 ; <== This is returned by the function

;; You can now simulate entering your age at the prompt by using with-in-str

user=> (with-in-str "34" (prompt "How old are you?"))
How old are you?
"34"                 ; <== The function now returns immediately 
南…巷孤猫 2024-10-22 10:22:52

这是我最终所做的示例代码。这个想法是一个简单的服务器读取/打印循环函数,它接受输入和输出流。我的问题是如何为这样的函数生成测试流,我认为字符串函数就可以了。相反,这就是我所需要的:

(ns test
    (:use [clojure.java.io :only [reader writer]]))
(def prompt ">")
(defn test-client [in out]
  (binding [*in* (reader in)
            *out* (writer out)]
           (print prompt) (flush)

(loop [input (read-line)]
             (when input
               (println (str "OUT:" input))
               (print prompt) (flush)
               (if (not= input "exit\n") (recur (read-line)) )
               ))))
(def client-stream (java.io.PipedWriter.))
(def r (java.io.BufferedReader. (java.io.PipedReader. client-stream)))
(doto (Thread. #(do (test-client r *out*))) .start)

(.write client-stream "test\n")

Here's sample code for what I ended up doing. The idea is a simple server read/print loop function which takes an in and out stream. My issue was how to generate testing streams for such a function, and I thought a string function would do. Instead this is what I needed:

(ns test
    (:use [clojure.java.io :only [reader writer]]))
(def prompt ">")
(defn test-client [in out]
  (binding [*in* (reader in)
            *out* (writer out)]
           (print prompt) (flush)

(loop [input (read-line)]
             (when input
               (println (str "OUT:" input))
               (print prompt) (flush)
               (if (not= input "exit\n") (recur (read-line)) )
               ))))
(def client-stream (java.io.PipedWriter.))
(def r (java.io.BufferedReader. (java.io.PipedReader. client-stream)))
(doto (Thread. #(do (test-client r *out*))) .start)

(.write client-stream "test\n")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文