Created
March 9, 2012 23:07
-
-
Save owainlewis/2009185 to your computer and use it in GitHub Desktop.
Clojure IO Protocols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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