Fork me on GitHub

Good day, I've noticed a bunch of dep's files in .m2/repository, the vast majority aren't related to any project in use, just the result of toying with different libs. What's the best practice to remove them?


I wouldn't bother doing anything with them

👍 2

I executed below code in repl it returning nil , how can I avoid returning nil if condition fail

(def a "B")

(when (= a "A")

Luke Zeitlin13:08:40

What do you want to return? (You can use 'if' rather than 'when' to allow for a specific failure case)


basically I am updating a variable using assoc statement, but I wanted to check few condition before that, which I have put in the when logic, when condition is false, it returning nil and acting as

(-> a 

Bob B13:08:24

But what do you want it to return? Expressions return something (even if it's nil). If you want to return something changed or something unchanged based on some condition, that's a case for if, or if you have a bunch of different conditions that can lead to different changes, maybe cond->(>)


@U013JFLRFS8, cond-> worked for me, thank you for your suggestion 🙂

Itay Dreyfus13:08:31

I'm gathering resources for learning Clojure here - Happy to add anything related 🙂


Ok gradually getting the hang of Clojure trying PEZ/rich4clojure problems on github (defn power "derive power set of in-set" ([in-set] (power in-set '(#{}))) ([[this-in-set & rest-in-set] power-res] (if this-in-set (recur rest-in-set (concat power-res (map #(conj % this-in-set) power-res))) (set power-res)))) In this power set generator I wondered if I could replace the concat with mapcat but couldn't find it. Is there a template which shows before code where mapcat applies?


(mapcat f coll) is equivalent to (apply concat (map f coll)). So it doesn’t apply here. Unless...


... unless you consider that for each item of power-res you do two things and that instead of doing these two thing separategly in two passes over power-res, you do them at once

(defn power
      "derive power set of in-set"
      ([in-set] (power in-set '(#{})))
      ([[this-in-set & rest-in-set] power-res]
       (if this-in-set
         (recur rest-in-set
                (mapcat (fn [sets] [sets (conj sets this-in-set)]) power-res))
           (set power-res))))


Grand thanks the apply bit was missing in my understanding when I read the 'doc'. I'll have to digest the mapcat fn conj re-write when Im less tired but it looks good.


(defn power [items]
  (reduce (fn [sets x] (into sets (map #(conj % x)) sets))  #{#{}} items))

🙌 1
Pedja Zolinsky16:08:47

How do I get the value of a key which is a character? The following returns nil

(get \a {\a 1 \b 1 \2)


get takes the associative item first


(get {\a 1 \b 1} \a) -> 1


get is more forgiving than the syntax


choose the style that you find easier to read


if the map is nil, then (nil \a) will throw a Null Pointer Exception, so usually people would do (get map \a)

👍 3

I find (get m k) easier to scan visually. I prefer: • (:key m) when I know the key (as a symbol) statically, • (get m k) otherwise.


I have a vector thats like [#uuid "00000000-0000-0000-0000-000000000000" :book "The Book Title" [:mystery :science-fiction]] .. is there a way to map the positions to field names in a let other than like like (let [:id (first book-vector) :type (nth 2 book-vector) :title (nth 3 book-vector) etc. etc.

Joshua Suskalo16:08:34

if you want to put it into a map there's also zipmap