Skip to content

Instantly share code, notes, and snippets.

@owainlewis
Created March 9, 2012 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save owainlewis/2009185 to your computer and use it in GitHub Desktop.
Save owainlewis/2009185 to your computer and use it in GitHub Desktop.
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