This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2023-05-04
Channels
- # announcements (1)
- # architecture (7)
- # beginners (44)
- # biff (11)
- # calva (15)
- # cider (5)
- # clerk (9)
- # clj-kondo (20)
- # clj-on-windows (19)
- # clj-yaml (2)
- # cljs-dev (39)
- # clojure (52)
- # clojure-czech (2)
- # clojure-dev (11)
- # clojure-europe (28)
- # clojure-hamburg (10)
- # clojure-hungary (3)
- # clojure-nl (1)
- # clojure-norway (59)
- # clojure-uk (5)
- # clojured (2)
- # clojurescript (33)
- # conjure (2)
- # datahike (1)
- # datomic (5)
- # defnpodcast (5)
- # emacs (18)
- # figwheel (2)
- # funcool (6)
- # graphql (1)
- # hyperfiddle (11)
- # jobs (3)
- # joyride (13)
- # malli (6)
- # music (4)
- # off-topic (45)
- # polylith (11)
- # practicalli (3)
- # rdf (3)
- # releases (1)
- # scittle (8)
- # shadow-cljs (13)
- # specter (2)
- # squint (8)
- # testing (6)
- # tools-deps (21)
- # xtdb (2)
is there a builtin x such that
(let [[a b c] [1 2 3]]
(x a b c))
gives you {:a 1 :b 2 :c 3}
?There are several macros in libraries that will do this...
e.g., https://github.com/worldsingles/commons/blob/master/src/ws/clojure/extensions.clj#L152 (that's ours from work)
(each macro is going to be slightly different but similar to what you need -- but I will say this is probably not considered very idiomatic 🙂 )
ok. one of those times this is not built in for a good reason perhaps?
Well, we have select-keys
and we have zipmap
(zipmap [:a :b :c] [1 2 3])
to achieve what you’d want, I would take a slightly different approach:
(let [ks [:a :b :c]
vs [1 2 3]]
(zipmap ks vs))
it requires a bit more typing but also lets you use nothing but the clojure stdlib
if you want to also produce bindings, then i would simply use {:keys [a b c]}
in a subsequent let
form, or wherever you end up using the produced mapok, what should I do about calling a js function with a cljs map that has :keys-like-this
?
Call cljs->js
on it?
Thanks @U04V70XH6. I discovered that in the context that prompted me to ask, which is using a native-js React component from Helix, I should do ($ ^:native NativeJsComponent)
I would like to write a function that can take a value that is either a simple string or an atom (specifically reagent flavored).. is there a predicate for determining if a value is a reagent atom (or any other type of atom)?
something like (instance? RAtom x)
should work
https://github.com/reagent-project/reagent/blob/master/src/reagent/ratom.cljs#L127
I would advise against RAtom
if you want to support regular atoms as well. Ratoms implement IAtom
, so this should work for both:
(def a (atom "Anton"))
(instance? clojure.lang.IAtom a)
Awesome! I have yet to use a vanilla atom in this project (reframe app) but I don’t want to unnecessary exclude it in case there is a future reason I need it. I suspect there may be performance reasons to use a regular atom if I don’t need to use the whole ‘rerender components that deref this’ (for like a trivial atom for backing a form component instertitially) but I don’t want to prematurely optimize, either
to test for IAtom you probably will need to use satisfies?
and in cljs scope clojure.lang.IAtom is not a thing unfortunately. but this
(satisfies? IAtom a)
should workbut I can omit the prefix, apparently 🙂 cljs.. juuuuuust different enough, but boy I’m smitten.
it reminded me of this: https://github.com/day8/re-com/blob/master/src/re_com/util.cljs#L26
Yeah that’s nice @U02F0C62TC1 thanks for the pointer. I need to give re-com another shake, I started using it in my first project and reverted to JS component library because I was a little overwhelmed by the granularity of re-com, but now I think I understand it a bit better! 🙂
The thing with satisfies?
is puzzling me now. From the docs it makes perfect sense to use it, yet clojure.core
has plenty of calls such as (instance? clojure.lang.ISeq x)
. It may be legacy since satisfies?
was only introduced in 1.2.
@U7ZSXG630 satisfies?
works a bit differently in clojure and clojurescript
@U04V4KLKC I'm wondering not about satisfies?
, but why instance?
works with java interfaces at all. Just asked a question in #C053AK3F9 about it.
instance works in java, there's no multiple inheritance, satisfies is for clojure protocols
AFAIK java interfaces are just abstract classes. It is completely matching the semantic of instance?
function. But I'll let to explain those details to anyone with more in-depth knowledge of Java internals.
If you're taking a string or an atom, are you only reading the contents of the atom, and not swapping anything into it? If so you might be better off using the IDeref protocol (https://cljs.github.io/api/cljs.core/#IDeref) which is implemented by anything that you can deref
/ @
.
Are there any resources on malli best practices? I can see the value in using it and I'm trying to use it in a server. However, the README at https://github.com/metosin/malli is very large and there are many ways to do many different things. To start with, I want a simple way to define the schema of input and output data, and I want it to give helpful errors if any caller gets it wrong. For example, I started by defining a schema and verifying input at the start of the function definition, like this:
(if-let [bad-input (me/humanize (explain-data example-schema input))]
(r/bad-request bad-input) ;; returns a nice error message to whoever called the endpoint incorrectly
...
But having to do this both at the start and something similar at the end of functions seems verbose. (Again, there are other ways suggested in the readme, but I want to know what the standard way to use Malli is)
This section looks convenient to me: https://github.com/metosin/malli/blob/master/docs/function-schemas.md#function-inline-schemas
But it's marked as experimental, implying to me that there are other ways that people do this that have been around for a long time.
How do people use Malli effectively? What are some good standards to have?Maybe ask this in #malli?
I'm looking at https://github.com/clojure/clojure/blob/4090f405466ea90bbaf3addbe41f0a6acb164dbb/src/clj/clojure/core.clj#L141 of clojure.core and wondering: how/why does instance?
work with java interfaces?..
(def
instance? (fn instance? [^Class c x] (. c (isInstance x))))
(def
seq? (fn ^:static seq? [x] (instance? clojure.lang.ISeq x)))
Not sure what you are asking. In Java, interfaces are also represented by the Class
class, and as you can see, Clojure’s instance?
just delegates to Class.isInstance(Object)
. Why wouldn’t that work with interfaces?
Can someone guide me in the right direction regarding some web dev / API stuff? Essentially trying to make a webapp that has a public database which needs unique identification to write to the database. It should also have an easy way to access it with an API or CLI. How would I go on about this? Been looking a bit at ring and reitit and haven't really been able to figure out how I'd add the authentication I need (partially also since I don't know what terms I'm looking for).
How far have you gotten so far? Or are you yet to get started?
Are you looking to build a pure REST-like API or a web app (with server-side HTML pages)?
It is dangerous from a security perspective to have an open database. Depending on what other systems you build to use the database, it can be catastrophic. Be very wary and don't be afraid to ask lots of questions when designing your architecture!
Gotten to the point of having a basic CRUD API without authentication and my commands to query the correct things in my DB
@U04V70XH6 Essentially what I am trying to build is a SPA (possibly, any alternative where the login prompt doesn't require a page change is good too) where some of the routes require auth, some dont, and I'm probably gonna use session auth since that that's the one i've figured out thus far, now I'm mostly having problems figuring out what the "correct" way is to implement stuff so it interacts with CLJS
For a stateless API, you would want to pass an auth token, e.g., JWT, with each API request from the front end (usually done as an authorization
header) and have the API look for that and deny any requests without a valid token -- except for the unprotected routes needed around displaying the home page/login form and handling login/password reset etc.
I don't know if there's really anything "standard" in the clj/s world but you can start off simple and get as sophisticated as you want, depending on how much security you need. Maybe consider OAuth 2 via Twitter or GitHub or something, depending on your audience?