Skip to content

Instantly share code, notes, and snippets.

@bnyeggen
Created December 29, 2011 14:36
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 bnyeggen/1534357 to your computer and use it in GitHub Desktop.
Save bnyeggen/1534357 to your computer and use it in GitHub Desktop.
Wrong and right way to parse a file in Clojure
;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