Hi from a new Clojurenaut

Hello everyone,

I’m learning Clojure atm, it’s my first lisp and functional language. And i really enjoy every moment :slight_smile: .
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?
  • what looks a clojure idiomatic way?
4 Likes

Hi, welcome to Clojure!

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.

  1. How are you running your code?
  2. Are you currently getting the behavior you want?

If you have a function like this,

(defn myfunc [opts]
  (prn "got opts:" opts))

you can show its use straight from the REPL,

user> (core/myfunc {:x 10})
"got opts:" {:x 10}
;; => nil

or through a command line invocation.

# from the same directory as your `deps.edn` file
$ clj -X core/myfunc :x 10
"got opts:" {:x 10}

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?!

  1. 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
  2. Yes

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:

(run! 
  (future (car (str x) :driving)) 
  (range 1 (inc n)))

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.