Created
December 29, 2011 14:36
-
-
Save bnyeggen/1534357 to your computer and use it in GitHub Desktop.
Wrong and right way to parse a file in Clojure
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
;dummy.csv is of format "1,2,3\n4,5,6\n" | |
;fails since everything is lazy (but works if you do something that forces resolution) | |
(with-open [r (reader "file:///home/brycen/dummy.csv")] | |
(for [lines (read-csv r)] | |
(zipmap [:x :y :z] lines))) | |
;Doesn't close "properly" (but works most of the time) | |
(for [lines (read-csv (reader "file:///home/brycen/dummy.csv"))] | |
(zipmap [:x :y :z] lines)) | |
;Dumps file, then parses it. Slurp handles closing. | |
(for [lines (read-csv (slurp "file:///home/brycen/dummy.csv"))] | |
(zipmap [:x :y :z] lines)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment