Fork me on GitHub

Anybody has a recipe on how to compose com.stuartsierra.component's in a clean way?


@funyako.funyao156 Components can be a solution to a problem, though there may be other solutions as well. What do you see as the problem you're facing?

Jp Soares12:07:06

Is anyone using with clojurescript? I tested importing one component and it seems that different components might need different ways to import. Are there some known issues or tips for using them with re-frame?


Not a clue, personally, but I’d suggest posting that one in #clojurescript and/or #re-frame. Good luck!

Jp Soares10:07:26

Yeah.. I posted in #clojurescript, but no answer 😞 I'll try re-frame, thanks.


Happened to run across this today, dunno if it may be helpful:


@ryan.russell011 iterate is interesting, given you’ve defined calculate-next-val. Cobbling together a naive solution based on that

(defn collatz' [n] (count (take-while #(> % 1) (iterate calculate-next-val n))))


that is much more of a functional approach than my original. Now I'm mad I didn't think of that lol. I've used all those components before.... Sometimes my brain clicks into being functional..and other times I super struggle to approach things in a functional way and default to my damn imperative habits ... grrrrr


Your solution is probably more efficient (especially in the event that the result is large)


if I'm understanding Clojure correctly.. would that be because the solution you provided is stack consuming.. while loop/recur is not?


Nah… the main concern with the (count (take-while ... approach is that it will actually produce and hold a sequence in memory, and then count it


(collatz' 989345275647) will create a sequence of length 1348


Well, actually, because of locals clearing count won’t actually hold it in RAM


An intermediate step, which uses iterate and parts of your solution is

(defn collatz'' [num]
  (reduce (fn [count n]
            (if (>= 1 n)
              (reduced count)
              (inc count)))
  (iterate calculate-next-val num))


(That one performs a lot like your raw loop/recur approach)


I like that solution. Whenever I see a loop/recur situation, my brain goes to reduce, and I couldn't figure out how to make that work with this problem. Thank you.. that was very help in seeing a few different alternatives to how I approached this problem!


is the usage of cond in my original solution idiomatic?


Are there any tricks to using set! with the threading macros? Right now I'm using as-> but curious if it can work with the thread-first macros or doto


set! is a macro which doesn't evaluate the first arg, so it won't work with many useful instances of thread-first or doto, but it can reasonably be used with thread-last


but style wise many would discourage using a macro like that inside a threading macro

👍 2