Fork me on GitHub
#beginners
<
2023-05-04
>
James Amberger04:05:29

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} ?

seancorfield04:05:10

There are several macros in libraries that will do this...

seancorfield04:05:05

(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 🙂 )

James Amberger04:05:08

ok. one of those times this is not built in for a good reason perhaps?

seancorfield04:05:08

Well, we have select-keys and we have zipmap

seancorfield04:05:19

(zipmap [:a :b :c] [1 2 3])

hifumi12306:05:43

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 map

James Amberger05:05:41

ok, what should I do about calling a js function with a cljs map that has :keys-like-this ?

seancorfield05:05:31

Call cljs->js on it?

James Amberger21:05:39

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)

1
Ryan11:05:21

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)?

avfonarev12:05:25

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)

Ryan12:05:38

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

delaguardo12:05:07

to test for IAtom you probably will need to use satisfies?

delaguardo12:05:32

and in cljs scope clojure.lang.IAtom is not a thing unfortunately. but this

(satisfies? IAtom a)
should work

Ryan12:05:58

Ooh thats nice, Yeah just figured out its cljs.core/IAtom

Ryan12:05:41

but I can omit the prefix, apparently 🙂 cljs.. juuuuuust different enough, but boy I’m smitten.

Ryan12:05:06

awesome! (satisfies? IAtom works for both a RAtom and a Atom, you all are the best!

Ryan12:05:48

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! 🙂

avfonarev13:05:33

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.

rolt13:05:31

satifies can be pretty slow if i remember correctly in some cases

delaguardo13:05:50

@U7ZSXG630 satisfies? works a bit differently in clojure and clojurescript

avfonarev13:05:38

@U04V4KLKC I'm wondering not about satisfies? , but why instance? works with java interfaces at all. Just asked a question in #C053AK3F9 about it.

rolt14:05:47

instance works in java, there's no multiple inheritance, satisfies is for clojure protocols

delaguardo15:05:27

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.

Ed11:05:12

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 / @.

Sam13:05:15

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?

Ferdinand Beyer14:05:10

Maybe ask this in #malli?

Sam14:05:04

Smart! Didn't see that.

avfonarev13:05:18

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)))

Ferdinand Beyer14:05:06

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?

👍 2
avfonarev19:05:08

@U922FGW59 My bad, did not know Java interfaces were represented by Class.

👍 2
Joshua Niemelä21:05:03

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).

seancorfield21:05:22

How far have you gotten so far? Or are you yet to get started?

seancorfield21:05:55

Are you looking to build a pure REST-like API or a web app (with server-side HTML pages)?

mloughlin21:05:52

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!

Joshua Niemelä22:05:53

Gotten to the point of having a basic CRUD API without authentication and my commands to query the correct things in my DB

Joshua Niemelä16:05:15

@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

seancorfield19:05:47

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.

seancorfield19:05:40

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?