3

Using code from this answer, I have

(defn repeat-image [n string]
  (println (apply str (repeat n string))))

(defn tile-image-across [x filename]
  (with-open [rdr (reader filename)]
    (doseq [line (line-seq rdr)]
      (repeat-image x line))))

...to tile an ascii image horizontally. Now, how would I be able to "ignore" the first line? The reason I'm doing this is each image has the coordinates (for example "20 63") as the first line, and I don't need the line. I tried some ways (keeping an index, pattern matching) but my approaches felt contrived.

Community
  • 1
  • 1
Emil
  • 447
  • 4
  • 18

1 Answers1

6

Assuming you'd like to skip the first line of the file and process the remaining lines as you do in tile-image-across, you can simply replace (line-seq rdr) with

(next (line-seq rdr))

In fact, you should probably factor out selecting the relevant lines and the processing:

;; rename repeat-image to repeat-line

(defn read-image [rdr]
  (next (line-seq rdr)))

(defn repeat-image! [n lines]
  (doseq [line lines]
    (repeat-line n line)))

Use inside with-open:

(with-open [rdr ...]
  (repeat-image! (read-image rdr)))

If instead your file holds multiple images and you need to skip the first line of each, the best way would be to write a function to partition the seq of lines into a seq of images (how that'd be done depends on the format of your file), then map that over (line-seq rdr) and (map next ...)) over the result:

(->> (line-seq rdr)
     ;; should partition the above into a seq of seqs of lines, each
     ;; describing a single image:
     (partition-into-individual-image-descriptions)
     (map next))

NB. with a lazy partition-into-individual-image-descriptions this will produce a lazy seq of lazy seqs; you'll need to consume them before with-open closes the reader.

Michał Marczyk
  • 83,634
  • 13
  • 201
  • 212