Fork me on GitHub
#beginners
<
2019-12-25
>
avfonarev13:12:14

Am I right that (transduce indentity f xs) is equivalent to (f (reduce f (f) xs)) ?

ackerleytng13:12:28

Working on advent of code, day 23. I created a bunch of input and output channels, so channels is of the shape {addr {:in-chan <channel> :out-chan <channel>}} , and then I have this router block, which i'm running in the repl over cider.

(let [channels (zipmap (range 50)
                         (repeatedly (fn [] {:in-chan (a/chan 10) :out-chan (a/chan 10)})))]
    ;; Boot all computers
    (doseq [[addr {:keys [in-chan out-chan] :as inout}] channels]
      (intcode-computer inout addr))

    ;; Do routing
    (while true
      (let [[msg _] (a/alts!! (map :out-chan (vals channels)))
            [addr x y] msg]
        (println "msg" msg)
        (if (= addr 255)
          (do (close-all channels)
              (println "=================================>>>" y))
          (do
            (println "routed" [x y] "to" addr)
            (a/onto-chan
             (get-in channels [addr :in-chan])
             [x y]
             false))))))
I'm pretty sure that :out-chan s are being written to, in go blocks with >! (I'm writing a vector to the :out-chans) but somehow alts!! doesn't seem to ever return; the println "rrrrrrrrrrrrrrrrrrr" never executes. Can someone help me out? Why doesn't the println execute?

hiredman17:12:56

Maybe use a pub

ackerleytng15:12:14

ah yes haha but the output is in the form [addr x y] while the next input is x then y so it was not direct to use pub and sub. i could try that, though.

ackerleytng15:12:20

are you also working on AOC?

ackerleytng15:12:15

so it seems like if I send a message in every iteration within the intcode-computer it works, but if i don't, something locks up

ackerleytng14:12:08

I figured alts!! isn't what i want since i'm not sure what happens to the other messages on the channel when one of them resolves, so i switched the implementation

(let [channels (zipmap (range 50)
                         (repeatedly (fn [] {:in-chan (a/chan 100) :out-chan (a/chan 100)})))]
    ;; Boot all computers
    (doseq [[addr {:keys [in-chan out-chan] :as inout}] channels]
      (intcode-computer inout addr))

    ;; Do routing
    (while true
      (let [[addr x y] (a/<!! (a/merge (mapv :out-chan (vals channels))))]
        (println "msg" [addr x y])
        (if (= addr 255)
          (do (close-all channels)
              (println "=================================>>>" y))
          (do
            (println "routed" [x y] "to" addr)
            (a/onto-chan
             (get-in channels [addr :in-chan])
             [x y]
             false))))))

Gulli14:12:35

They were talking about the conj function on #clojure earlier. Can be seen here: https://stackoverflow.com/questions/59463747/how-does-clojure-bind-variable-parameters/59465336#59465336 Why if fn being used in the def's body (seems it can be removed without messing anything up)? Is it because of the :static metadata?

bronsa15:12:16

the static metadata does nothing, it's a vestigial thing

bronsa15:12:46

(def foo (fn .. is like writing (defn foo ..

bronsa15:12:01

conj is defined before defn exists in clojure.core

Kari Marttila15:12:48

I just finished watching @ericnormand 's excellent "REPL Driven Development" course in https://purelyfunctional.tv/. I really recommend to purchase that course if you are learning Clojure or you want to learn more productive REPL driven development practices as I did. I'm not a complete newbie in Clojure but I must say that I learned a lot watching a seasoned Clojurian working with the code and REPL and explaining what he was doing and why. That is definitely something you cannot learn just by reading Clojure books.

12
Gulli19:12:18

Does he use Emacs?

Kari Marttila19:12:55

He uses mostly Emacs + Cider and is very fluent with that tool. But he occasionally also uses IntelliJ IDEA + Cursive. But the editor is not the main point, not at all. He demonstrates good REPL practices that you can use what ever editor you choose to use.

Gulli19:12:37

I might check this out. Thanks!

Kari Marttila19:12:28

Another great resource is Stuart Halloway's Repl Driven Development presentation. I started immediately using a repl scratch file to edit my repl experiments instead of using the repl editor. https://vimeo.com/223309989

👍 8
Gulli19:12:55

Would you say http://PurelyFunctional.tv would be suited for someone that's a total Clojure newbie?

Gulli16:12:45

@bronsa Yeah my bad, I actually read that as 'defn', but it's actually 'def'

Hi17:12:02

How to check if file exists?

seancorfield17:12:01

(.exists (io/file ...))

4
Kari Marttila19:12:21

Anyone knows a good book or course about topic "Clojure standard library hidden gems"? 🙂

jaihindhreddy22:12:37

I just go to http://clojuredocs.org and peruse all the stuff in clojure.core. You could read the docstrings straight from your REPL but, there are crowd-sourced examples for these things on that site that I find very useful. That, and looking at other people's solutions on 4clojure also taught me a lot of interesting ways in which the core fns compose together.

Gulli20:12:07

Anyone here subscribe to Manning liveBook?

Kari Marttila21:12:03

Yes! Definitely going to buy that Clojure book as well!