This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-07-21
Channels
- # announcements (11)
- # architecture (8)
- # aws (7)
- # babashka (1)
- # beginners (55)
- # calva (52)
- # cider (4)
- # clj-kondo (5)
- # clojure (12)
- # clojure-europe (7)
- # clojure-uk (3)
- # clojurescript (40)
- # clr (1)
- # conjure (5)
- # data-oriented-programming (7)
- # datomic (8)
- # emacs (3)
- # events (1)
- # graphql (2)
- # honeysql (5)
- # lsp (7)
- # missionary (24)
- # nbb (10)
- # off-topic (12)
- # pathom (5)
- # reagent (9)
- # reitit (4)
- # schema (1)
- # sci (9)
- # shadow-cljs (2)
- # specter (6)
- # tools-deps (4)
- # xtdb (13)
Idiomatic clojure question about conditionally adding elements to a new array. Is it better to do this:
(cond-> []
conditionA (conj :a)
conditionB (conj :b))
or
(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 conditionsThis 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
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
(assoc-some
:key1 (some-> val1 not-empty)
...)
;; Compare to
(cond->
(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.
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
with:
path: /root/.m2/repository
key: ${{ runner.os }}-maven-${{ hashFiles('shadow-cljs.edn') }}
restore-keys: |
${{ runner.os }}-maven-
heya @U7AMPCPU2, https://github.com/clj-commons/rewrite-clj/blob/fd518e24ab2c5123091bb230a8a52bc8688bd636/.github/workflows/unit-test.yml#L29-L36
Thanks
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™.
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
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)
2
user=> (get [1 2 3] 99)
nil