This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-21
Channels
- # announcements (26)
- # aws (1)
- # babashka (40)
- # beginners (36)
- # calva (9)
- # cider (38)
- # clara (5)
- # clj-commons (4)
- # clj-kondo (29)
- # cljs-dev (8)
- # cljsrn (2)
- # clojars (12)
- # clojure (151)
- # clojure-europe (16)
- # clojure-gamedev (1)
- # clojure-nl (2)
- # clojure-uk (7)
- # clojurescript (2)
- # copenhagen-clojurians (2)
- # datalevin (18)
- # fulcro (7)
- # graphql (7)
- # gratitude (9)
- # helix (2)
- # honeysql (3)
- # introduce-yourself (1)
- # jobs (1)
- # lsp (13)
- # malli (10)
- # nextjournal (2)
- # off-topic (13)
- # pathom (1)
- # pedestal (2)
- # portal (4)
- # remote-jobs (1)
- # ring-swagger (1)
- # shadow-cljs (21)
- # specter (1)
- # testing (2)
- # tools-build (6)
- # vim (2)
- # xtdb (5)
1. Is there a way to thread the (optional) on-change such that the (or ... (fn []))
goes away, or is the no-op fn fine as is?
(defn text-input [{:keys [attrs subscription dispatch]}]
(let [local (r/atom (or @subscription ""))
on-change (or (:on-change dispatch) (fn []))]
(fn []
[:input.input
(merge attrs
{:type :text
:on-change #(->> (.. % -target -value)
(reset! local)
(on-change))
:value @local})])))
2. Assuming 1 is yes, is there a cleaner way to do this nested de-structure? I saw nested de-structures on http://clojure.org but not with :keys being used at the parent level and my attempts to 'guess' at the syntax all failed:
(defn example [{:keys [attrs sub dispatch]}]
(let [on-blur (:on-blur dispatch)
on-change (:on-change dispatch)]
...))
about 2. you could further destructure in the let
(defn example [{:keys [attr sub dispatch]}]
(let [{:keys [on-blur on-change]} dispatch]
...))
Actually this works
(defn example [{:keys [attr sub dispatch]
{:keys [on-blur on-change]} :dispatch}]
(println attr sub dispatch on-blur on-change))
about 1. you can do this
(def dispatch { :on-blur 2})
(let [a 1 {on-change :on-change :or {on-change 3}} dispatch]
(println a on-change))
=> 1 3
sorry the above is not what you asked, but I think you can use cond->>
(defn text-input [{:keys [attrs subscription] {on-change :on-change} :dispatch}]
(let [local (r/atom (or @subscription ""))]
(fn []
[:input.input
(merge attrs
{:type :text
:on-change #(cond-> %
true (other-fn -target -value)
true (reset! local)
on-change (on-change))
:value @local})])))
(changed your .. to other-fn
as it seems you can do a thread first rather than thread lastohhh I tried to do the second destructure example you showed but I think I was using 'dispatch' instead of ':dispatch' for the inner destructure, that makes sense though thanks for figuring it out!
And I was unaware of the cond-> threading so that's cool to see as well. Thanks so much Antonio!
Not executing the function is the best approach, but in case you encounter this again, all forms of get
allow for a “not-found” value. So instead of:
(or (:on-change dispatch) (fn []))
you can write:
(:on-change dispatch (fn []))

The primary difference here is that the value you pass is not evaluated if you don’t need it and you use or
. So you definitely wouldn’t use this form for:
(:on-change dispatch (some-really-expensive-fn))
@U051N6TTC thanks for pointing out the optional defaults- I'm coming over from Java and can't get over (in a good way!) the number of convenience overloads that Clojure sneaks in haha. Do you happen to know what the use case is in not having default value producer short-circuited?
Most of the time when you’re doing a lookup (via get
, keyword-as-fn, or map-as-fn) then returning nil
works just fine, but if you do need a default value, then it’s typically a constant, e.g. :not-found
. In that case, it’s perfectly fine to provide it as an argument. Using or
to short circuit something away is just useful when there’s some kind of operation that’s going to happen that you want to avoid
If "namespace a" requires "namespace b" and "namespace b" requires "namespace a" will that cause any issues? If it doesn't, why not? Does it have to do with how the compiler works?
Ok thanks @U06BE1L6T
Hi, I have a function I want to use to update
a set of values for given keys in a map - but I don’t want to create the keys if they don’t already exist. Currently I’m doing this line by line for each key that I want to update
, like so:
(update m :key-to-update fn-to-apply)
How do I a) not create the kv in the map m
if it’s not already there, and b) once I have done that, combine multiple such conditional updates in a single line (e.g. if I have key-to-update-2, key-to-update-3
etc.?
Thanks in advanceif you need args (defn update-maybe [m k f & args] (if (contains? m k) (update m k #(apply f % args) m))
(defn update-maybe [m k f & args] (if (contains? m k) (update m k #(apply f % args)) m))
(a missing paren fixed)
@dpsutton @tero.matinlassi @nobita.0918
Here is a https://viebel.github.io/klipse-embed/?src=KGRlZm4lMjB1cGRhdGUtbWF5YmUlMjAlNUJtJTIwayUyMGYlMjAlMjYlMjBhcmdzJTVEJTIwJTBBJTIwJTIwKGlmJTIwKGNvbnRhaW5zJTNGJTIwbSUyMGspJTIwJTBBJTIwJTIwJTIwJTIwKHVwZGF0ZSUyMG0lMjBrJTIwJTIzKGFwcGx5JTIwZiUyMCUyNSUyMGFyZ3MpKSUyMG0pKSUwQSUwQShwcmludGxuJTIwKHVwZGF0ZS1tYXliZSUyMCU3QiUzQWElMjAxJTdEJTIwJTNBYSUyMGluYykpJTBBKHByaW50bG4lMjAodXBkYXRlLW1heWJlJTIwJTdCJTNBYSUyMDElN0QlMjAlM0FiJTIwaW5jKSk%3D&lang=clojure of update-maybe
in action.
you could go with medley’s update-existing
http://weavejester.github.io/medley/medley.core.html#var-update-existing
Hey, how can I correctly declare a function that takes another function as a parameter? For example, i tried
(defn make-eta [nu t1 t2 t]
(* (- t t1) (- t t2) (nu t)))
With my attempt it almost worked, problem is nu is a different function with t as a parameter. What I'm trying to implement is a function make-eta
that takes parameters nu, t1, t2
and returns the function eta(t) = (t - t1) (t- t2) nu(t). So t1, t2 are just numbers and nu is a different function that takes t as a parameter. So, correctly calling the procedure I think should look like
((make-eta nu 't1 't2) 't)
instead of the way I'm calling it in the picture.
Should have clarified also, I'm using SICMUtils library(?) (don't know yet what's the correct term)functions are not handled specially, they are passed like any other argument
I see. But the problem I have is say for example a function g takes as parameter t and I want to implement a function f that takes as parameter g but not t
so something like
(defn f [g]
(fn [t] (g t))
returns a new function taking t and applying gif I understand you correctly
(of course that simplifies to (def f g)
but you could have other logic in there)
I worked! Thanks for the help. The answer was
(defn make-eta [nu t1 t2]
(fn [t] (* (- t t1) (- t t2) (nu t))))
you are not doing clojure stuff, you are using some dsl in clojure for math that does some equation rendering, and dsl that si, either it, or whatever the rendering thing is, doesn't understand higher order functions
Oh I see. I think the key is higher order functions, haven't learned about those yet. I should look into those.
in clojure those just work, and functions are values that can be passed and returned no different from the number 5
there is a #sicmutils so if whatever issue you are having is with that library, and not with clojure in general, you may get better help there
@dpsutton @tero.matinlassi @nobita.0918
Here is a https://viebel.github.io/klipse-embed/?src=KGRlZm4lMjB1cGRhdGUtbWF5YmUlMjAlNUJtJTIwayUyMGYlMjAlMjYlMjBhcmdzJTVEJTIwJTBBJTIwJTIwKGlmJTIwKGNvbnRhaW5zJTNGJTIwbSUyMGspJTIwJTBBJTIwJTIwJTIwJTIwKHVwZGF0ZSUyMG0lMjBrJTIwJTIzKGFwcGx5JTIwZiUyMCUyNSUyMGFyZ3MpKSUyMG0pKSUwQSUwQShwcmludGxuJTIwKHVwZGF0ZS1tYXliZSUyMCU3QiUzQWElMjAxJTdEJTIwJTNBYSUyMGluYykpJTBBKHByaW50bG4lMjAodXBkYXRlLW1heWJlJTIwJTdCJTNBYSUyMDElN0QlMjAlM0FiJTIwaW5jKSk%3D&lang=clojure of update-maybe
in action.