Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lazy-seq println side-effects are printed as expression result #2621

Open
guyguy2001 opened this issue Sep 13, 2024 · 1 comment
Open

lazy-seq println side-effects are printed as expression result #2621

guyguy2001 opened this issue Sep 13, 2024 · 1 comment
Labels
bug Something isn't working evaluation

Comments

@guyguy2001
Copy link

guyguy2001 commented Sep 13, 2024

I made the mistake of using for instead of doseq for running side effects multiple times in a row, which (to the best of understanding) resulted in the side-effects not happening immediately, instead they only happen when evaluating the result.

The problem is that when evaluating it with Ctrl+Enter, the println output is shown as part of the string representation of the output, instead of how it's supposed to be:

(def inventory {:wood 2 :iron 2})

(defn show-item [type amount] (format "%s: %d" (name type) amount))

(defn simplified-draw-inventory
  [separator inventory]
  (for [[type amount] inventory]
    (println separator
             (show-item type amount))))
(comment
  (def x (simplified-draw-inventory "\n--------\n" inventory))
  x
  x
  (println "foo")
  :rcf)

image

In each screen part, the first red square is the lazy-seq version, and the second is running println directly.
I can see the output terminal's behavior being something unavoidable as long as you want to show the string representation as it's being generated, but the inline output's behavior is weird to me - evaluating (println "foo") doesn't show foo at all, while if that happens as a side-effect of transforming the result into a string (such as with lazy-seq), it does appear there!

(It originally happened when calling simplified-draw-inventory directly, I bound it to x just for the example)

@PEZ PEZ added bug Something isn't working evaluation labels Sep 13, 2024
@PEZ
Copy link
Collaborator

PEZ commented Sep 13, 2024

Yeah, that's weird.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working evaluation
Projects
None yet
Development

No branches or pull requests

2 participants