clojurelazy-evaluationlazy-sequences

Trying to split string in Clojure running into lazy seq problem


I am working on a problem to read in a file with lines like:

A abcdefg
B bcdefgh

But I keep getting errors about Lazy Sequence not compatible with Java Charseq ..

I tried:

(def notlazy (doall lyne2))

Then thought I verified:

(realized? notlazy)
true

But still:

(str/split notlazy #" ")
ClassCastException class clojure.lang.LazySeq cannot be cast to class
  java.lang.CharSequence (clojure.lang.LazySeq is in unnamed module of
  loader 'app'; java.lang.CharSequence is in module java.base of loader
  'bootstrap')  clojure.string/split (string.clj:219)

Help please!


Solution

  • The first argument to str/split must be a CharSequence to be split. Presumably you want to split each input line in the sequence for which you can use map without needing to eagerly evaluate the input sequence:

    (map (fn [line] (str/split line #" ")) lyne2)