Force realization

Hey,

I have coded quite a lot with Clojure already but I can’t get forced realization of a lazy seq to work. I already do this:

blocks (doall (into (group-subroutine-decs subroutine-dec-code)))

Sometimes, into forces the realization and I can easily debug my code. Sometimes doall does the trick (as the ClojureDocs say). In this case, nothing works. I can’t really debug the code because the value simply isn’t realised (IntelliJ).

Is there any trick I can use? I’d appreciate some help here

Both of them realize the seq, into [] also changes the type to a vector.

In this case, nothing works. I can’t really debug the code because the value simply isn’t realised (IntelliJ).

What are the actual symptoms? What makes you think that whatever you’re seeing is due to collection realization and not something else?

Well, I can’t really debug the code because I cannot see the value because it is not realised yet…

You just repeated the OP without any details.

What exactly do you see? Can you share a screenshot?
What makes you think that values aren’t realized?
Can you create a small reproducible example?

Here’s what I see, everything works as expected on my end. The only thing I don’t like here is that the Explore Elements link immediately realizes the lazy seq without prompting, but not a big deal.

not-realized cannot be expanded at all without changing the default viewer via RMB → View as.
realized can be fully expanded and all the items are there, same for as-vector.

(Don’t pay any attention to that :gap - it’s a common issue when debugging Clojure where a breakpoint ends up being triggered earlier than where it was put, for some reason.)

Hm, interesting. I did manage to enforce the realisation with this method, but I’ll always have to add it to any seq that might be lazy:

(defn pr2
  "Print + Return"
  [x]
  (prn x)
  x)

I currently try to write my result into a file, but the output file looks like this:

clojure.lang.LazySeq@a9ff4363()clojure.lang.LazySeq@30f30684()clojure.lang.LazySeq@8ecc864b()clojure.lang.LazySeq@d22be15d()clojure.lang.LazySeq@8ecc864b()clojure.lang.LazySeq@8ecc864b()clojure.lang.LazySeq@749aa280()clojure.lang.LazySeq@7771210d()clojure.lang.LazySeq@7bf6fda3()clojure.lang.LazySeq@abdb17a0()

The top level caller does this:

(defn compile-jack
  [filepath]
  (let [tokens (tokenize filepath)
        compiled (doall (compile-class tokens))]
    (result-vm-to-file filepath compiled)))

So neither the doall, nor the spit in result-vm-to-file seem to realize the result. I feel like I misunderstand something about doall pretty badly.

You are misunderstanding what you are seeing:

This is how a general Java object prints in Clojure, unless there is specific machinery to print it in a different format.

doall realizes a lazy sequence but it is still a LazySeq object so that is how it is going to print by default.

If you want to print the elements of a realized lazy sequence, you’ll need to either do so explicitly or “pour” it into a vector.

You don’t show what compile-class does but if you do not need a lazy sequence here, don’t use one: produce a vector instead.

Does that help?