Unexpected behaviour with 'repeatedly' #29
-
Hi, first off I'd just like to say I'm a shameless fan of all the work you do, Michiel. I'm amazed by all the creative and practical contributions you've made to the Clojure ecosystem! Recently I watched a YouTube video by Veritasium where he explains the 100 Prisoners riddle. I implemented a quick simulation on CodePen using Scittle to satisfy myself that I understood what I saw in the video. I tested the solution in a Clojure REPL, and in ClojureScript with Planck first and it worked just fine, but it didn't when I put it up on CodePen. The run-simulation function didn't work as expected until I changed it from:
to:
This seems wrong to me, and the modified function doesn't work in normal REPL's. I haven't worked with ClojureScript very much, so I assume there's something about Reagent or Scittle that I don't yet understand. I will be very grateful to anyone who can explain why the original run-simulation function doesn't work as expected, and why my kludgy workaround appears to fix the problem! Once we're all done, you're very welcome to include my code with the Scittle examples if you think it's worthy of sharing. Let me know if you do and I'll make sure I leave it alone. Kind regards, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi Paul, Thanks a lot for your kind words! The problem is that (defn run-simulation [n]
(doall (repeatedly n simulation))) but since you're not using the result, you could also just use (defn run-simulation [n]
(dotimes [i n] (simulation))) Your workaround triggered the evaluation of the lazy sequence which is why that worked as well, but it does raise an exception (visible in the developer console). |
Beta Was this translation helpful? Give feedback.
Hi Paul,
Thanks a lot for your kind words!
The problem is that
repeatedly
return a lazy sequence and nothing happens unless you realize the sequence:but since you're not using the result, you could also just use
dotimes
:Your workaround triggered the evaluation of the lazy sequence which is why that worked as well, but it does raise an exception (visible in the developer console).