I’m learning Clojure atm, it’s my first lisp and functional language. And i really enjoy every moment .
My primary learning source is the book Clojure for the Brave and True. After the chapter of concurrency i got inspired and tried to solve a old university java example with clojure.
I’m really interested to get some feedback to improve my clojure skills.
Is there a way to wait & notify (like java) for futures?
Clojure for the Brave and True is great – that was my introduction to Clojure too. Sharing some code and asking for feedback is a great way to improve.
I’m not quite sure I understand what you want your code to achieve. I tried running (core/main 40), but I can’t seem to get any output. And I don’t feel like I can provide any useful feedback if I don’t know what you want to achieve.
Hi,
with the code i tried to model a parallel behavior, just to concrete the knowledge from the book.
The code itself models a parking garage with 10 slots and 40 cars, all in a separate thread try to enter the garage. Because there are not enough slots they have to try again to get a place (infinite looping). The state of the slots is managed in a global atom, not sure if this is the best approach?!
I run the code in vscode (calva) repl, i just run the main method from the comment block.
I refactored it, now it should be possible to run to code with clj -M -m core 40
Welcome to Clojure and Calva! That code looks fine to me. A global atom is what I would use for this too. In your -main function you could use run! as an alternative to loop since you don’t care about a return value, but loop does the job too. That would look something like: