On Jan 10, 12:22 am, wubbie <[email protected]> wrote:
> How can you add line numbers for each line printed from the file.
> Without line number, I have this:
>
> (with-open [rdr (reader "executors.clj")]
> (filter #(println %) (line-seq rdr)))
I don't believe there's a function to do this, but it's easy enough to
add one:
(defn zip-index [coll]
(map vector coll (iterate inc 0)))
This will create a sequence of items paired with their indexes:
=> (zip-index [:a :b :c])
([:a 0] [:b 1] [:c 2])
Next, instead of filter, you probably want doseq. The filter function
filters a collection according to a predicate. The doseq function
applies a function with side effects (such as println) to each item in
a collection.
(with-open [rdr (reader "executors.clj")]
(doseq [[line index] (zip-index (line-seq rdr))]
(.println System/out (str (inc index) " " line))))
- James
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---