Fork me on GitHub
#beginners
<
2017-07-04
>
drewverlee02:07:53

I’m constantly amazed by the answers i see on 4clojure. http://www.4clojure.com/problem/131

(fn [& sets]
  (let [p-set (fn [ss] (->> (map (fn [v] #(->> v (conj %2) (conj %1))) ss)
                            (reduce #(reduce %2 %1 %1) #{#{}})))]
    (->> (map p-set sets)
         (map #(disj % #{}))
         (map #(map (partial apply +) %))
         (map set)
         (apply clojure.set/intersection)
         (empty?) (not))))
That one is a bit of a head scratcher, but at least i see what the confusing part is doing. With some answers i dont know where to start. I think maybe in browser answers might not have produced the most readable code. At least i hope thats the reason i can’t read some of these -_-.

tap04:07:06

For me, one advantage of this dense clojure code over other languages I have worked on in the past is that I can comment out some part of it and the code is still runnable. What I normally do when I got this chuck of code snippet that I don’t understand what it’s doing is to set this up in my editor with connected repl. Then I comment out most of the lines except the first one (In this case, (->> (map p-set sets))). Evaluate it to see the result in the repl. Trying to understand it, uncommment the next line, eval it, try to understand it and repeat until it’s all uncommented.

tap04:07:57

I think it requires a bit of behavior change to always keep in mind that I now have the repl close to my hand. It’s so easy to run a code snippet especially when there’s no external dependencies or side-effects. I don’t have to fully rely on my brain to compute the whole code snippet anymore

vinai13:07:39

Is there an idiomatic way to print a value in a threading macro (`->` or ->>) and then return the identity? Of course I can defn my own function, but I think I saw something clever once and forgot it...

noisesmith13:07:26

with -> doto kind of works

vinai13:07:38

I think I had something else in mind but that is good to know already!

noisesmith13:07:54

another approach is

(-> x (f) (g) (as-> % (do (println "foo:" %) %)) (h))