Lazy creation of infinite vectors
hesokuri.main> (source ring.util.io/piped-input-stream)
(defn piped-input-stream
"Create an input stream from a function that takes an output stream as its
argument. The function will be executed in a separate thread. The stream
will be automatically closed after the function finishes.
e.g. (piped-input-stream
(fn [ostream]
(spit ostream \"Hello\")))"
[func]
(let [input (PipedInputStream.)
output (PipedOutputStream.)]
(.connect input output)
(future
(try
(func output)
(finally (.close output))))
input))
Get private fields (some code taken from here)
(require 'clojure.reflect)
(def p (partial + 1 2 3))
(clojure.reflect/reflect p)
(defn get-private-field [instance field-name] (. (doto (first (filter (fn [x] (.. x getName (equals field-name))) (.. instance getClass getDeclaredFields))) (.setAccessible true)) (get instance)))
(get-private-field p "f")
(get-private-field p "arg1")
(comment
(defn pipe-process-stream [in out]
;; This way of copying is probably wrong, since cjio/copy may block
;; before the buffer is full. Fix this. Maybe use InputStream.available?
(future (try (loop []
(let [c (.read in)]
(when (not= -1 c)
(doto out
(.write c)
.flush)
(recur))))
(finally (.close in)))))
(defn pipe-process [invoke-streams-args in out]
(ctl/info "invoking: " invoke-streams-args)
(let [[sub-in sub-out :as sub] (apply git/invoke-streams invoke-streams-args)]
(pipe-process-stream in sub-in)
(pipe-process-stream sub-out out)
(git/log sub))))