I'm trying to realize a lazy sequence (which should generate a single string) in the REPL, with no luck. The original code works fine:
(def word_list ["alpha" "beta" "gamma" "beta" "alpha" "alpha" "beta" "beta" "beta"])
(def word_string (reduce str (interpose " " word_list)));
word_string ; "alpha beta gamma beta alpha alpha beta beta beta"
But not wanting to leave well enough alone, I wondered what else would work, and tried removing the reduce
, thinking that str
might have the same effect. It did not...
(def word_string (str (interpose " " word_list)))
word_string ; "clojure.lang.LazySeq@304a9790"
I tried the obvious, using reduce
again, but that didn't work either. There's another question about realizing lazy sequences that seemed promising, but nothing I tried worked:
(reduce str word_string) ; "clojure.lang.LazySeq@304a9790"
(apply str word_string) ; "clojure.lang.LazySeq@304a9790"
(println word_string) ; "clojure.lang.LazySeq@304a9790"
(apply list word_string) ; [\c \l \o \j \u \r \e \. \l \a \n \g \. \L \a \z \y...]
(vec word_string) ; [\c \l \o \j \u \r \e \. \l \a \n \g \. \L \a \z \y...]
(apply list word_string) ; (\c \l \o \j \u \r \e \. \l \a \n \g \. \L \a \z \y...)
(take 100 word_string) ; (\c \l \o \j \u \r \e \. \l \a \n \g \. \L \a \z \y...)
The fact that some of the variations, gave me the characters in "clojure.lang.LazySeq" also worries me - did I somehow lose the actual string value, and my reference just has the value "clojure.lang.LazySeq"? If not, how do I actually realize the value?
To clarify: given that word_string
is assigned to a lazy sequence, how would I realize it? Something like (realize word_string)
, say, if that existed.
Update: based on the accepted Answer and how str
works, it turns out that I can get the actual sequence value, not just its name:
(reduce str "" word_string) ; "alpha beta gamma beta alpha alpha beta beta beta"
Yes, this is terrible code. :) I was just trying to understand what was going on, why it was breaking, and whether the actual value was still there or not.