This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-02-23
Channels
- # announcements (2)
- # atom-editor (3)
- # babashka (49)
- # beginners (100)
- # biff (9)
- # calva (78)
- # clj-kondo (18)
- # clojure (143)
- # clojure-europe (13)
- # clojure-germany (1)
- # clojure-nl (2)
- # clojure-spec (5)
- # clojure-sweden (2)
- # clojure-uk (4)
- # clojurescript (58)
- # conjure (1)
- # cursive (4)
- # datascript (11)
- # datomic (63)
- # docker (7)
- # emacs (18)
- # events (1)
- # fulcro (18)
- # graalvm (5)
- # helix (4)
- # improve-getting-started (13)
- # jobs (4)
- # jobs-discuss (3)
- # lsp (15)
- # malli (90)
- # membrane (14)
- # off-topic (12)
- # other-languages (5)
- # pedestal (7)
- # polylith (53)
- # re-frame (15)
- # reitit (23)
- # releases (4)
- # remote-jobs (9)
- # ring (11)
- # shadow-cljs (90)
- # specter (2)
- # testing (3)
- # tools-build (63)
- # vim (2)
- # xtdb (8)
Hi, How do I check whether the CLJS data structure has key? otherwise add it I want's to check the key 'ghi' in below
{
'abc': 'first',
'def': {
'j': 'sub'
}
}
If not exist add it as below
{
'abc': 'first',
'def': {
'j': 'sub'
},
'ghi': {
'k': 'test'
}
}
Please help.Yes. Here is the CLJS structure
{:abc "first" :def {:j "sub"}}
And want's to add like below if 'ghi' does not exist
{:abc "first" :def {:j "sub"} :ghi {:k "test"}}
The merge
approach is reasonable only when the second map is small and its real type doesn't matter.
In any other situation, you should use contains?
with assoc
.
Does assoc/update returns new map? If yes I wants to update the existing map
Clojure[Script] data structures are immutable. You're supposed to return a new map and use it instead of the old one.
If at a single, very specific place, you can't practically use that paradigm, you can store your map in an atom
and swap!
it with the function you want.
Thanks for answering! Can we assign back the returned map to same map?
No, but you use the atom everywhere instead of directly using the map. Since it seems like functional programming is a new thing for you, I encourage you to read https://www.braveclojure.com/functional-programming/ or https://www.learn-clojurescript.com/section-4/lesson-21-functional-programming-concepts/ and any other materials you can find on the topic when applied to Clojure.
Also note that there's #beginners, which is the most appropriate channel for any kind of entry-level questions.
hi, i have a newbie question.
i am trying to use halfmoon
in my cljs project, which is based on shadow-cljs.
https://www.gethalfmoon.com/docs/download/
and install it using npm
(yarn in fact) like the following as the document suggests:
npm install halfmoon
the above doc says that once installed it can be loaded by the following two lines:
require("halfmoon/css/halfmoon-variables.min.css");
const halfmoon = require("halfmoon");
i think the second line can be expressed in cljs (please let me know if wrong)
(:require ["halfmoon" :as h] ...)
but i am not sure what i should do about the first line require
ing the css
file in cljs. Or maybe should i copy/paste the line in index.html
file?
can someone enlighten me here? thanks!The simplest way is to just refer to the CSS file directly in your index.html
, maybe copying the file some place out of node_modules
- depending on how you deploy your project.
Can a macro modify the ns
form of the namespace it’s called in?
yes, basically. I’m trying to figure out how to balance stuff like lazy loading and SSR
shadow’s reader conditionals make it possible in a way but I don’t really like reader conditionals in a mostly cljs code base, just seems like a lot of cruft and a bit of an “invasive implementation” detail that will hurt readability & comprehension
Wondering of shadow build hooks could help with it but seems like in general its gonna be a bit of a lift to make it work
Hi all, I am struggling to write a (pretty basic) CLJS macro which uses compile-time constants (e.g. goog-define
) in order to emit different code - for instance:
(defmacro with-feature
[id enabled disabled]
(if (contains? app.config/features id)
`~enabled
`~disabled))
where do I put that macro so that the compiler doesn't complain it can't resolve app.config/features
?
I realise it has to go in a CLJ namespace, but it needs visibility of app.config/features
which is in a CLJS namespace
Is that even possible?
many thanks in advance...
Thanks for your reply, but that doesn't seem enough. I'm requiring the namespace everywhere, but I still get: failed to require macro-ns "app.config", it was required by "app.config"
Error in phase :compile-syntax-check
FileNotFoundException: Could not locate app/features__init.class, app/features.clj or app/features.cljc on classpath.
ah sorry that's the wrong error
[1] failed to require macro-ns "app.config", it was required by "app.config"
[1] Error in phase :compile-syntax-check
[1] RuntimeException: No such var: app.config/features
I'd deal with the first line first - sounds like it could be the cause for the other errors.
So I have a CLJS namespace with a declaration like the following:
(ns app.config
(:require [clojure.string :as str]
[app.util :as util])
(:require-macros [app.config])) ;; <======
Then I have a CLJ namespace with the same name containing only the macro shown earlier (no requires)
It seems that the CLJS namespace cannot require the CLJ macro namespace because the macro contained in it references unresolved Vars (i.e. app.config/features
)
Ah, wait, my bad - I read the macro incorrectly.
In your code, you use app.config/features
in macro expansion time. You have to quote the outer form, so move the syntax quotes outside of if
.
well yes I can do that and it works, BUT then I'm emitting the check for runtime
i would like to do the check at compile time
because everything i need to decide which code to emit is a compile-time constant
Right. Things defined with goog-define
are compile-time constants indeed. But macro expansion is done before CLJS compilation. :)
So sounds like you'd have to either stop using macros or switch from goog-define
to something else.
Note that DCE works just fine with goog-define
if you have the right tags - so there might not be any need in macros.
I don't just want to elide code...i'm trying to write a rudimentary feature-flagging system...I want to be able to say: when this feature is enabled emit this expression, otherwise emit this other expression.
What I want is fairly similar to what is shown here: https://cljs.github.io/api/cljs.core/goog-define
the difference is that instead of when
I want to use my own macro
so are you saying that a CLJ macro cannot use any CLJS vars at macro-expansion time even if these are compile-time constants?
Because macro expansion is an earlier step. Unless you define your goog constants in a way that can be introspected with CLJ code.
I see...do you happen to have the slightest idea how that might look?
hmm i could use env-variables like shown here: https://stackoverflow.com/questions/60413100/how-to-pass-env-variables-to-clojurescript-app
hmm interesting
I will try that - many thanks for your time :thumbsup:
yes i do