Fork me on GitHub
#clojurescript
<
2021-03-17
>
pez09:03:50

There is no way I can require in a macro dynamically like I can do in Clojure, right?

pez10:03:43

Thanks. I’ll have to find the most ergonomic way to do it lacking that. 😃

p-himik12:03:21

What exactly do you want to achieve? It's likely that a solution is on the surface, so you won't have to spend too much time.

pez13:03:44

Thanks for caring! I wrote this macro yesterday that is like let and then also taps the bindings. For debugging purposes. It’s reasonably ergonomic to use in Clojure. But in ClojureScript it becomes a bit clunky. Still easier than manually putting together the thing to be tapped, but anyway. Would like it to be easier to use. Here’s the macro: https://github.com/PEZ/taplet

p-himik13:03:48

If your CLJC file contains only macros then it can be just CLJ - that reader conditional at the ns form can just be removed. Apart from that, I'm still not sure what you want to achieve. What do you want to require conditionally?

p-himik13:03:24

Ah, immediately after writing this I realized that switching from CLJC to CLJ will make your users use :require-macros. Right, CLJC is better in this case. Or just a combination of CLJ and CLJS where the CLJS file has nothing but the ns form.

pez14:03:06

CLJC or CLJ+CLJS is a matter of taste probably. I think CLJC signals that it is a feature targeted at both.

pez14:03:26

What I want to achieve… The README of the repo only targets CLJ usage. The CLJS howto is a bit clunky, since needs to be put in the ns form, so I held off writing that part until I had investigated the options some.

p-himik14:03:23

Ah, you meant requiring taplet itself, I see. Honestly, I wouldn't change anything in your README. By the time anyone decides to use tap>, IMO they should already know their target platform more or less. I.e. in this case they should be able to convert that (require '[...]) form to an appropriate section in their ns form themselves, in their head. Otherwise, you'll also have to make distinctions between flavors of platforms. E.g. (require '[...]) in self-hosted CLJS is (or should be) perfectly fine.

pez14:03:55

Thanks. I’ll skip patronizing the reader then. 😃 .

zackteo13:03:47

What is the go-to library for http calls/requests in ClojureScript? Like the equivalent of axios in JS

cjsauer13:03:32

I think cljs-Ajax is the most active lib for that: https://github.com/JulianBirch/cljs-ajax For extremely simple use cases tho I’ve gotten away with simply interop’ing with js/fetch

3
zackteo13:03:34

Very uninformed question but isn't ajax no longer really used (at least in the JS world). What might I be ignorant to here :x ?

zackteo13:03:00

Was thinking http-kit might be the standard but seems to just be for clojure https://github.com/http-kit/http-kit

p-himik14:03:06

AJAX is still used. Well, mostly its "AJ" part, but still. I can't imagine it going anywhere in our lifetime. Perhaps, you meant that JS has introduced the new fetch API that is supposed to replace XMLHttpRequest. But the fetch API is still AJAX. And XMLHttpRequest is not going anywhere any time soon either.

athomasoriginal14:03:01

I honestly just use fetch 🤷

athomasoriginal14:03:25

Is there a particular feature set your looking for?

isak14:03:40

fetch will not let you cancel a request if you need to, IIRC

isak14:03:15

We just use an internal wrapper around .XhrIo , but cljs-ajax looks good.

athomasoriginal15:03:08

> fetch will not let you cancel a request if you need to I believe this changed recently with https://developers.google.com/web/updates/2017/09/abortable-fetch :thumbsup:

isak15:03:37

Ah, nice!

zackteo01:03:38

Thanks everyone for your inputs 😄

emccue15:03:43

Has anyone used figwheel and solved the "loading 100 files for 5 minutes on refresh" issue?

emccue15:03:01

like if on initial load everything was concatenated together that would solve everything

vanelsas16:03:07

I have a map that looks like this {:filename "filename" :zip-file zipfile} where zipfile is a file read in with .readAsArrayBuffer I want to validate this map with cljs.spec.alpha. How can I do that? I'm unsure how to handle the zip file

p-himik17:03:54

I don't think there's any other feasible approach than to just check that it's a not empty ArrayBuffer.

vanelsas17:03:04

yeah thought about something like that. Thanks, will give it a try

vanelsas13:03:10

I've ended up doing it with a custom function:

(s/def :test/array-buffer #(= (type %) js.ArrayBuffer))

👍 3
Aaron Goh20:03:42

does anyone know the best way to go about checking in a list of dictionaries if some of the key values match up (i.e. if I have [{:a 3, :d 4}, {:a 2, :d 4}, {:a 4, d: 4}], I want to do something like return true since all the values for key d is 4)

localshred21:03:45

Think you want to use every?

(every? #(= 4 (:d %1)) list-of-maps)

localshred21:03:53

assuming you know the value to check for ahead of time

localshred21:03:30

if you just wanted to know if the same key across the maps have the same value (regardless of what it is), you could do something more like

Aaron Goh21:03:48

I’m more trying to see that values match rather than if they equal something specific

localshred21:03:15

(->> list-of-maps (map :d) (set) (count) (= 1))

localshred21:03:53

ah, so just do all the maps in the list have the same keys and values?

localshred21:03:29

(let [m (first list-of-maps)]
  (every? #(= m %1) (rest list-of-maps)))

localshred21:03:59

sorry if I'm still misunderstanding 🙂

Aaron Goh21:03:26

no worries, I’m actually trying to clarify my own usecase really quickly but this is definitely really helpful

localshred21:03:53

cool cool lmk when you know more

👌 3
rgm20:03:48

I have a set of maps to maintain, and do occasionally need an order on it. So I had the (semi-?) bright idea of using metadata like ^{:pos 1}. But I also have to serialize the set into browser localstorage. I’ve been using edn so far, and it seems that edn/read-string will pull the metadata back in OK but … what should I use to make the edn string? Seems (pr-str ,,,) isn’t cutting it

rgm20:03:16

oh wait nvm … just rubber-ducked it for myself:

user=> (binding [*print-meta* true] (pr-str ^{:foo 1} {:bar 2}))
"^{:foo 1} {:bar 2}"

rgm21:03:54

hah, well, after all that I entirely forgot that stashing the positions in metadata would make it invisible to reagent, so no re-renders on position changes. So much for my first “I know I’ll solve that problem with metadata” brainwave.

alexmiller21:03:04

a large percentage of those end badly :)

rgm22:03:22

more or less often than my “I know I’ll solve this with a macro” brainwaves? 😜

andy.fingerhut21:03:36

If you can use a ordered sequencel/list/vector rather than a set, you'll get your ordering guarantees, but I have no idea whether the feature in reagent you are trying to use supports that.

andy.fingerhut22:03:38

I'm sure you already considered and rejected that for some perfectly valid reason. Just call me captain obvious 🙂

rgm22:03:10

yeah, I just wanted to be clever and hold them as a set for easy membership checks but honestly it’s 10-12 maps and I can just trivially turn them into a set when I need to.

rgm22:03:10

yeah, I just wanted to be clever and hold them as a set for easy membership checks but honestly it’s 10-12 maps and I can just trivially turn them into a set when I need to.

macrobartfast23:03:58

for whatever reason, a slurped file containing

[:p "foo"]
is not rendering with hiccup with
(html (slurp "page"))
where html is :refer’d hiccup; it works fine with
(html [:p "foo"])
thoughts?

phronmophobic23:03:45

slurp returns a string in code, [:p "foo"] is a vector

phronmophobic23:03:13

try: (type (slurp "page)) vs (type [:p "foo"])

phronmophobic00:03:39

you probably want something like:

(with-open [rdr ( "page")
            pb-rdr (java.io.PushbackReader. rdr)]
  (clojure.edn/read pb-rdr))

phronmophobic00:03:10

but there are several ways to read an edn file depending on the exact semantics required

macrobartfast00:03:40

thanks! string and vector respectively… it balks at a conversion of the string to a vector via vec, however.

macrobartfast00:03:51

It’s not technically an edn file.

phronmophobic00:03:54

well, you need some way to parse the file. what type of file is it?

macrobartfast00:03:54

throws

[ is not a valid element name.

phronmophobic00:03:23

check (prn (vec (slurp "page")))

macrobartfast00:03:25

in this case, no type, just a plain file.

phronmophobic00:03:33

does the file contain edn?

phronmophobic00:03:30

if it contains edn, then clojure.edn/read should work

macrobartfast00:03:46

just the plain vector.

macrobartfast00:03:55

just

[:html [:p "hello"]]
which
(prn (vec (slurp "page")))
turned into
[\[ \: \h \t \m \l \space \[ \: \p \space \" \h \e \l \l \o \" \] \]]

macrobartfast00:03:30

and when it’s fed into hiccup, it complains that

[ is not a valid element name.

phronmophobic00:03:37

the above code with the edn reader should work

macrobartfast00:03:45

ok, I can give that a go.

phronmophobic00:03:44

you'll probably need to require:

(require '
          'clojure.edn)

macrobartfast00:03:42

seems to be available, but that throws

java.lang.String cannot be cast to java.io.PushbackReader

macrobartfast00:03:24

so weird. basically, I just can’t coerce the file into a proper vector, it seems.

phronmophobic00:03:42

are you copied the code correctly?

phronmophobic00:03:03

for eg.:

(with-open [rdr ( "deps.edn")
            pb-rdr (java.io.PushbackReader. rdr)]
  (clojure.edn/read pb-rdr))
This works for me

phronmophobic00:03:15

another way to read edn data from a file is (read-string (slurp "page")) although it will cause issues for some edge cases

macrobartfast00:03:05

I got it!

(read-string (slurp "page"))

toot 6
macrobartfast00:03:38

and totally grateful for the help!

👍 3