with-in-str

clojure.core

  • (with-in-str s & body)
Evaluates body in a context in which *in* is bound to a fresh
StringReader initialized with the string s.

1 Example top

  • ;; 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 
    
Log in to add / edit an example.

See Also top

Log in to add a see also.

Plus_12x12 Minus_12x12 Source clojure/core.clj:4177 top

(defmacro with-in-str
  "Evaluates body in a context in which *in* is bound to a fresh
  StringReader initialized with the string s."
  {:added "1.0"}
  [s & body]
  `(with-open [s# (-> (java.io.StringReader. ~s) clojure.lang.LineNumberingPushbackReader.)]
     (binding [*in* s#]
       ~@body)))
Vars in clojure.core/with-in-str:
Used in 0 other vars

Comments top

No comments for with-in-str. Log in to add a comment.