Fork me on GitHub
#beginners
<
2022-08-29
>
2FO00:08:05

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?

hiredman01:08:10

I wouldn't bother doing anything with them

👍 2
popeye13:08:50

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

(def a "B")

(when (= a "A")
  "testing") 

Luke Zeitlin13:08:40

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

popeye13:08:35

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 
     nil)

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->(>)

popeye13:08:25

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

Itay Dreyfus13:08:31

I'm gathering resources for learning Clojure here - https://www.are.na/itay-dreyfus/clojure-0rhdjyippgc Happy to add anything related 🙂

2
stantheman13:08:54

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 ..map with mapcat but couldn't find it. Is there a template which shows before code where mapcat applies?

cgrand14:08:27

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

cgrand14:08:05

... 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))))

stantheman14:08:49

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.

cgrand14:08:20

(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)

dpsutton16:08:11

get takes the associative item first

dpsutton16:08:01

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

pppaul17:08:36

get is more forgiving than the syntax

pppaul17:08:05

choose the style that you find easier to read

escherize22:08:04

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

👍 3
teodorlu09:09:59

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.

Ryan18:08:21

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