This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-09-10
Channels
- # announcements (9)
- # babashka (19)
- # beginners (39)
- # calva (7)
- # cherry (1)
- # cider (2)
- # clojure (31)
- # clojure-europe (8)
- # clojure-norway (9)
- # datalevin (10)
- # events (2)
- # fulcro (10)
- # hyperfiddle (9)
- # joker (1)
- # lsp (50)
- # membrane (34)
- # minecraft (2)
- # missionary (21)
- # off-topic (17)
- # pedestal (1)
- # polylith (8)
- # reitit (3)
- # sql (4)
- # squint (16)
- # xtdb (14)
Is this the idiomatic way of defining a constant?
(def ^:const constant2 1.2)
just def
, in Clojure, everything is constant by default.
:const
has other implications regarding compilation
as far as I understood adding :const
will improve the runtime. thus best practice?
No, it has not. I just wanted to learn and apply the best possible option 🙂 .
Is there a reason why I should not make use of :const
?
Yes, because some things can't be made into a const and then the code which uses them will fail and you'll scratch your head. The best possible option is to start with def Then if you build an application use direct linking Then check if you have performance issues Then profile Then realize you're probably blocking on an IO thread or non optimal algorithm, fix it and go have beer It's not that const has no use, but it's rarely required. The defaults are good :)
Thx for the info! 🙂
What kinds of things cannot be made into a const?
ref types, I think, java types. they can be made into a const, but every piece of code using this const will break
(defn str-count-chars [s char]
(->> (seq s) (take-while #{char}) count))
(str-count-prefixes "## fo" \#)
(defn positions [pred coll]
(keep-indexed (fn [idx val] (when (pred val) idx)) coll))
(count (take-while (fn [[x y]] (= x y)) (map-indexed vector (positions #{\#} "## fo #"))))
2 attempts at counting the heading level in markdown. Any tips? I want to count the occurrence of a char at the beginning of a string, then I thought there must be something general with positions
(count (second (re-find #"^(#*)" s)))
(count (take-while #(= \# %) s))
something like that ?
(defn restargs [name & args]
(println name args (type args))
(let [{default :hello} args]
default))
(restargs "restargs hello" :hello 3000)
Why can we destructure the args
, even though the type of args
is clojure.lang.ArraySeq
?
Edit:
Destructuring works, I'm curious why it works, even though the args
are not a clojure.lang.PersistentArrayMap
I assume that args can be multiple things, thus due to ambiguity it does not work. just guessing :)
Destructuring works, I'm curious why it works, even though the args
are not a clojure.lang.PersistentArrayMap
Sorry, misread 🙃
You can apply map destructuring to sequential collections (like rest args) as if those were the kvs
This is sometimes called kwarg destructuring which allows you to pass alternating keyword / value options at the end of your function call, then destructure them like a map on the rest args
I would like to know why was the evaluation (in the REPL) of this function successful?
(defn add-sales-tax [cart-item]
(let [{:keys [price]} cart-item]
assoc cart-item :sales-tax (* price tax-rate)))
My assumption is that assoc
, cart-item
, :sales-tax
and (* price tax-rate)
are all expressions and only the evaluation of the last expression is getting returned.
Am I right or is there another reason why it was successfully evaluated?
This map
(def cart [{:name "Silicone Pancake Mold" :price 10.49}
{:name "Small Pour-Over Coffe Maker" :price 18.96}
{:name "Digital Kitchen Scale" :price 24.95}])
It's seems that you are right,
(def tax-rate 3)
(add-sales-tax {:price 4}) // => 12
was actually (map add-sales-tax cart)
🙂
And thx for the clarification!
So I have to be more careful with my parentheses 🙂 .
Hello friends! I'm reading book "Clojure for brave and true" and find in chapter 1 some strange mutation: after command "`lein new app clojure-noob`" Leiningen named folder like clojure_noob. Why folder name with _ not with - ? Thnx!
Java can't handle dashes thus underscores are used when defining the structure of the project.
I have a small dataset (a timetable) that I originally wrote row-wise. I realized it makes a lot more sense for it to be column-wise. So I came up with this solution
(->> (-> "times.edn"
slurp
read-string
(get-in [:weekday :southbound])
vals
flatten)
(partition 15))
I'm wondering if nesting thread macros like this is a faux pas or if this is ok. any suggestions/improvements welcome in either caseit is, of course, subjective, but I sort of get the impression that it's generally frowned upon.<https://stuartsierra.com/2018/07/06/threading-with-style> explicitly calls this out as a "don't". And in this case in particular, the partition call is the only thing being "thread-lasted", so a couple viable options include just calling partition directly on the thread-firsted form (e.g. move partition 15
to replace ->>
) and using a let
interesting read, thank you
Not sure if this will apply to your data structure but a few times I've been able to use this really elegant way of turning rows into columns: (map apply vector my-data)
I found in the clojuredocs for map
: https://clojuredocs.org/clojure.core/map#example-542692cac026201cdc326b01 (it's the last example in this small block of examples)
you might be able to replace vals
and then flatten
with (mapcat vals)
but that's a quick untested thought (edit: this wouldn't work in the thread first expression of course)
Yes, you should probably not use flatten
: https://stuartsierra.com/2019/09/25/sequences-in-flatland
that map apply vector
solution is beautiful