I am writing my first Clojure program.
I am using clojure.data.csv to process a csv file. My file is potentially large and so I do want to exploit laziness. My MWE code to demonstrate my issue is shown below.
When I execute the load-data function, I get "IOException Stream closed" and so it is clear to me that the lazy stream is being closed before the point of consumption.
I have looked over the documentation for data.csv (https://github.com/clojure/data.csv) and can see that one way to prevent the stream from being closed before consumption is to move the stream opening to the callstack where the stream is consumed. As far as I understand it, this is what I have done below since (take 5) is within the confines of with-open. Clearly, I have a conceptual gap. Deeply appreciate any help!
(ns data-load.core
(:gen-class)
(:require [clojure.data.csv :as csv]
[clojure.java.io :as io]))
(defn load-data [from to]
(with-open [reader (io/reader from)
writer (io/writer to)]
(->> (csv/read-csv reader)
(take 5))))