Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clojure IO Protocols
(defprotocol IO
"A protocol for reading and writing"
(read-file [this])
(write-file [this]))
(extend-protocol IO
String
(read-file [file]
(with-open [rdr (clojure.java.io/reader file)]
(reduce conj [] (line-seq rdr))))
(write-file [text dest & opts]
(with-open [w (clojure.java.io/writer dest ~@opts)]
(.write w text))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.