Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I create an input stream that reads from a string instead of a file or url

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

like image 312
zippy Avatar asked Jan 31 '11 19:01

zippy


1 Answers

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 
like image 189
sanityinc Avatar answered Oct 16 '22 23:10

sanityinc