Fork me on GitHub
Ovidiu Stoica09:07:21

Idiomatic clojure question about conditionally adding elements to a new array. Is it better to do this:

(cond-> []
  conditionA (conj :a)
  conditionB (conj :b))
(remove nil? 
    [(when conditionA :a)
     (when conditionB :b)])
or is there a better solution? The current case is for declaratively making an xml node with children Like so
(xml-node :name 
      [(xml-node :kid1)
       (xml-node :kid2)])
But some nodes should only be included on conditions


This also depends on what those conditions are. Without knowing more, I would probably pick cond-> - that looks more clear to me. Btw. there's also #idioms channel

Ovidiu Stoica10:07:42

Did not know about the idioms channel, Ty! Indeed the conditions can be 1-2 lines and now writing, I see that it would be cumbersome to say true to all the ones that go in by default without a check. Ty


I recently used this approach:

;; From Medley
  :key1 (some-> val1 not-empty)

;; Compare to
  (seq val1) (assoc :key1 val1)
My case was a little special though: • The value and key names were long, making repeating them in cond-> uncomfortable • All of my conditions could easily be expressed as a fn in clojure.core that returns the value or nil (e.g. not-empty) • In some cases, I needed to run transformations on the values before assoc’ing them, which was easy to do like (some-> val not-empty another-fn) In some cases I also needed to do a nested assoc, so I ended up writing an assoc-some-in helper fn to replace Medley’s assoc-some


It also occurs to me that your XML thing looks remarkably similar to Hiccup. You might be able to use hiccup directly, but if you can’t, their approach was to just automatically remove nil from all of the vectors.

👍 1

What’s the proper way of caching .m2 files in github actions? I have this, but it’s not working:

- name: Maven cache
        uses: actions/cache@v2
          path: /root/.m2/repository
          key: ${{ runner.os }}-maven-${{ hashFiles('shadow-cljs.edn') }}
          restore-keys: |
            ${{ runner.os }}-maven-

Sameer Thajudin20:07:57

This stumped me. Invoke an array with an integer and it returns the element in the position that the integer defines. ([1 2 3] 2) returns 3. The docs don’t seem to mention it. What is the idea behind this?


Lots of clojure datastructures implement clojure.lang.IFn and they tend to do what makes sense™.

👍 1

maps look up the item provided ({:a 1} :a) -> 1 (#{:a} :a) -> :a as it checks membership and returns the value if its in the set

👍 1

It's also worth noting that vectors are associative on their indices:

user=> (ancestors (type [1 2 3]))
#{clojure.lang.ILookup java.lang.Iterable clojure.lang.IEditableCollection clojure.lang.Associative ...
user=> (get [1 2 3] 1)
user=> (get [1 2 3] 99)

👍 1