Fork me on GitHub
#clojure
<
2023-06-07
>
jaide06:06:52

Are the State of Clojure survey results for this year posted somewhere or are they still being processed?

🔖 10
Alex Miller (Clojure team)13:06:38

apologies, I have been swamped. I did actually do some more work on the writeup last week and this week, trying to get it done

jaide16:06:20

No problem! There is a lot on your plate, just wanted to make sure I didn't miss it

Alex Miller (Clojure team)17:06:06

it is one of things that is never quite the most important thing :)

😄 2
mal23:07:01

Might be a good use case to delegate the task to code interpreter :D

souenzzo10:06:09

I'm writing a custom IPersistentMap that renames the key if it already exists in the map, like this

(assoc (my-custom-map)
  :a 1
  :b 2
  :a 3
  :a 4)
#_#_=> {:a 1 :b 2 :a-1 3 :a-2 4}
Does anyone know if there is a library that already does this? If not, what would be a good descriptive name for this map?

p-himik10:06:33

What would use-cases for such a map be?

delaguardo10:06:03

I did something similar to track changes for a map but without renaming: custom type with parent field, in case assoc is about to overwrite some key new map is returned having original in the parent. It was called TrackingMap

souenzzo10:06:31

> What would use-cases for such a map be? (log/info "my-message" {:random :data} ex) I wanna be able to merge :random :data with (ex-data ex), and other sources (like Mapped Diagnostic Context etc), without lost data or getting it too nested.

Ed11:06:08

> I wanna be able to merge :random :data with (ex-data ex), and other sources Isn't that what namespaced keywords are for?

igrishaev11:06:41

I would highly NOT recommend making such a map. It's completely unobvious and requires one's time to understand what's going on. Instead, write a function that takes a seq of pairs and builds such a map. It would be a trivial reduce closed over a set of keys. It would also save you several days from inventing and testing your "smart" stuff.

Ed11:06:57

maybe something you could just nest the data?

(log/info "my-message" {:data {:random :data} :ex ex})

souenzzo11:06:38

> Isn't that what namespaced keywords are for? Yes, but from the log library side can't assume that everyone that creates an ex-info will use qualified keys.

vemv11:06:43

> If not, what would be a good descriptive name for this map? ClashFreeMap 😄

souenzzo11:06:13

@U1WAUKQ3E OK, implemented a clash-free-merge, using the @U45T93RA6 naming. In fact, I hope that this is a single-usage function.

potetm11:06:57

No no. You misunderstood. If you use namespaced keys when you make the ex-info, you won't have any collisions.

potetm12:06:41

If you're making a library, ::kw is especially useful for avoiding collisions. Especially if you use reverse domain convention in your namespace name.

igrishaev12:06:45

a quick and dirty version:

(defn clash-free-map [pairs]
  (loop [map-idx {}
         map-acc {}
         [[k v :as pair] & pairs] pairs]
    (if pair
      (if-let [idx (get map-idx k)]
        (let [k-new
              (-> k str (subs 1) (str "-") (str idx) keyword)]
          (recur (update map-idx k (fnil inc 0))
                 (assoc map-acc k-new v)
                 pairs))
        (recur (update map-idx k (fnil inc 0))
               (assoc map-acc k v) pairs))
      map-acc)))
samples
(clash-free-map [[:a 1] [:b 2]])
{:a 1, :b 2}

(clash-free-map [[:a 1] [:a 2]])
{:a 1, :a-1 2}

🙌 2
minikomi02:06:47

yeah I'd reach for a function before creating a whole new datatype

valerauko11:06:10

this sounds to me like diffing a change in a persistent map