Fork me on GitHub
#beginners
<
2019-11-29
>
lockdown-02:11:20

is there a cider "pprint to the repl buffer" command?

lockdown-02:11:45

nevermind, found something better

dromar5602:11:45

Maybe that's what you found, but C-c C-p can be really handy.

lockdown-02:11:03

yeah, knew that one but it doesn't output to the rep,l

dromar5602:11:21

Oh, so cider-pprint-eval-last-sexp-to-repl

lockdown-02:11:17

but the better one I found is cider-pprint-eval-last-sexp with a prefix arg

keymone08:11:55

possibly dumb question, but why :keys option map destructuring isn't allowed to be a map of bindings to default values? why repeat same names in :or?

jeremys12:11:24

Hi, I have something puzzling me I’d like to share. I wanna have reflection warnings always enabled on some project I am working on. To do so I have been advised to use the user.cljns as a way to set up this kind of config to be automatically loaded during development. Here is where I am a bit confused. In user.cljif If (set **warn-on-reflection** true) the reflection warnings are only enabled in the user ns. If I (alter-var-root #'*warn-on-reflection* (constantly true)) reflection warnings won’t work in the userns but will work everywhere else... What would be the clean and proper way to have reflections warnings enabled for everything ?

andy.fingerhut14:11:41

So it is not sufficient for your purposes to have it enabled everywhere except in the user namespace?

andy.fingerhut14:11:06

Have you tried both the set! and the alter expressions in the user.clj file?

jeremys16:11:21

hi! It is perfectly sufficient for me to have it enabled everywhere but the user.clj file. I was wondering why this behaves the ways it does particularly the alter-var, and if directly fiddling this way with var state wasn’t bad practice!

jeremys16:11:11

@ also using both unables the warning really everywhere. I guess am just puzzled why the alter doesn’t work in the ns it’s called in. It’s interesting 🙂

andy.fingerhut16:11:45

I do not have in my head already a brief explanation why. I do know that warn-on-reflection is one of a very small number of Vars that are pushed/popped in the Clojure compiler in some basis, per file if I recall correctly. Also set! only changes the current thread’s value for a Var, not all threads. Alter-var-root changes the “root” value of a Var, which is independent of the per-thread values a var may have

jeremys16:11:50

yes it must not be a simple thing to figure out! thanks Andy.

andy.fingerhut17:11:43

Well, I think understanding it fully can be broken up, analyzed, and explained in simple steps, but I suspect the number of those simple steps might be a few dozen.

jeremys18:11:56

yeah, making things simple doesn’t make them easy...

jeremys18:11:13

before using the user.clj trick actually looked a lot to find a way to specify those global values as argument to clojure’s main, or env variable... it turned out that there might be one global that can be set via java properties. It wasn’t warn-on-reflect 🙂

jeremys12:11:48

@keymone hey mate, having :keysas a map would force you to have default values for everything you destructure, with the :or you supply defaults only where you want them.

jeremys12:11:11

also it allows for defaut values to be applied to nay binding you are creating not just the :keys one {n :not-n :keys [a b] :or {n 1 a 2}}

keymone12:11:54

sure, but it could be an option

keymone12:11:14

i mean, it looks nice with :keys [a b] :or [a 1 b 2] but in real world those are not single letter vars and repeating a dozen of them in keys and in or is quite verbose, i'd rather have a bunch of nils in the :keys section instead

carladrago13:11:29

hey there is anyone experienced with the environ library? I'm not sure if the way i'm using it to return a default when an env var isn't found is wrong.

keymone13:11:14

have used it, wouldn't say experienced, show the code?

carladrago14:11:38

i'm accessing an env var doing this:

(env :env-var "")

carladrago14:11:09

the idea being that if :env-var is empty, it returns an empty string ""

carladrago14:11:45

i'm not sure if this is correct, or, in the case where there is an :env-var, it simply adds an empty string and returns something like: "the env var" ""

keymone14:11:47

env is just a hasmap, i would be surprised if (env :var "") would have any side effects or change any state

carladrago14:11:30

okay - thanks

imparato.andrea14:11:05

hello,question: is there a way to call a defrecord method dynamically like (.f record) where f is passed as parameter?

keymone14:11:06

@imparato.andrea you can treat records as maps: ((keyword f) record)

imparato.andrea14:11:47

interesting! thx a lot

ablazevix17:11:51

hey everyone! a few days ago, I had a question about changing the content-type header to csv. I got some great help and was able to do it. Now, I would like to make some changes on the client side, so we get a prompt with a downloaded csv file as seen below.

ablazevix17:11:22

I assume it's going to be the reg-event-fx part, particularly in the :http-xhrio :response-format part. I tried looking through the docs but wasn't able to find how to change it to download prompt

shaun-mahood17:11:26

@ablazevix Here's what I used recently in a project - it wasn't re-frame specific and I brought in an extra dependency, but hopefully it gives you a place to start

ablazevix18:11:29

awesome, thanks for the lead! so, in your experience this was the most efficient way to do it?

shaun-mahood18:11:13

(defn download-csv [filename content]
  (let [mime-type (str "text/csv;charset="
                       (.-characterSet js/document))

        blob (new js/Blob
                  (clj->js [content])
                  (clj->js {:type mime-type}))]
    (js/saveAs blob filename)))

shaun-mahood18:11:10

Then I used something like [:button {:on-click #(download "file.csv")}] to trigger the download

shaun-mahood18:11:06

Which you should be able to put in to your :on-success function instead

ablazevix18:11:40

ohhhh.. that's neat!!

shaun-mahood18:11:45

I have no idea if it's the most efficient way, but it has been working well for what we wanted to do with it. Based on https://eligrey.com/blog/saving-generated-files-on-the-client-side/ it looks like there is a standard Filesaver interface in the W3C File API, so you could probably use that instead if you didn't want to bring in the dependency.

ablazevix18:11:06

yea I read through the docs, and I realized that perhaps I don't need to figure this out on the client side, so instead I am trying to add a content-disposition header.

ablazevix18:11:30

(response/header (response/content-type base-response-map "text/csv") "Content-Disposition" "attachment/download.csv"))

darrell18:11:05

Finally I have a question where I can provide some code. I’m generating some mock data using existing mock data:

(def research-claims
  (map (fn [id company-id user-id is-completed] {:id id
                                                 :company_id company-id
                                                 :user_id (:id user-id)
                                                 :is_completed is-completed})
       (let [max-ids (count company-ids)]
         (map rand-id (range max-ids))
         company-ids
         (take max-ids (mk-users basic-user-ids basic-role))
         (repeatedly max-ids #(= (rand-int 2) 0)))))
The existing mock data are things like company-ids and (mk-users basic-user-ids basic-role) but those have different lengths so I’m trying to generate my mock data based on the one with the smallest count (`company-ids`). Originally I had (count company-ids) in place of max-ids without the let but I realized I could define max-ids in a let and reuse that. However, when I run that and eval research-claims I get the following error:
Error printing return value (ArityException) at clojure.lang.AFn/throwArity (AFn.java:429).
Wrong number of args (1) passed to: my-app.utils.seed-data/fn--27217
My first question is; is there a trick to reading that error that will tell me which function I’m passing the wrong number of args to? Barring that, can anyone see where my problem lies?

hiredman19:11:00

Expressions result in a single value

hiredman19:11:00

The result of your let is the final seq created with repeatedly

eggsyntax02:11:40

When you get an exception like this, it can be really useful to do (clojure.repl/pst) to print the stack trace of the most recent exception. In this case, the first line of the stack trace (after it prints the exception that you’ve already seen) should point to clojure.core/map, which would give you a clue that the function getting the wrong number of args is the fn being mapped.

dpsutton18:11:01

you are only passing a single arg to the top level function you are mapping with. (fn [id company-id user-id is-completed]...) expects four args and is receiving only one.

dpsutton18:11:34

if you just eval

(let [max-ids (count company-ids)]
         (map rand-id (range max-ids))
         company-ids
         (take max-ids (mk-users basic-user-ids basic-role))
         (repeatedly max-ids #(= (rand-int 2) 0)))
in the repl you'll see that only returns the last expression not the four collections you are expecting

darrell19:11:41

:face_palm:

dpsutton19:11:48

[email protected]:~/projects/clojure/lsp4j$ clj
Clojure 1.10.1
user=> (map (fn [x y] (inc x)) (range 3))
Error printing return value (ArityException) at clojure.lang.AFn/throwArity (AFn.java:429).
Wrong number of args (1) passed to: user/eval1/fn--137
(user=> (map (fn my-function [x y] (inc x)) (range 3))
Error printing return value (ArityException) at clojure.lang.AFn/throwArity (AFn.java:429).
Wrong number of args (1) passed to: user/eval140/my-function--141
(user=> 

dpsutton19:11:13

and the error is difficult to read because you are using a function without a name. look at the error messages here

darrell19:11:45

Thanks @dpsutton and @hiredman!

ablazevix20:11:00

ahhh, so as it turns out, adding a :content-disposition var to the header won't work with :xhrio (noted)

ablazevix20:11:59

Instead, I am trying to have add an anchor [:a] that would link to the localhost:8080 and commence the download, but for some reason it is not working.. anyone knows why this would not work?

noisesmith20:11:22

dumb question: does adding the "http://" at the front explicitly change anything?

noisesmith20:11:12

because if that was on http://localhost, the link might resolve as http://localhost/localhost:8080 etc. - as if it were a relative link

ablazevix20:11:12

we have a winner ladies and gents!!

ablazevix20:11:18

thanks a lot @noisesmith

noisesmith20:11:31

np, it was a shot in the dark

ablazevix20:11:45

do you have night vision 🙂

noisesmith20:11:20

years of suffering working on a front-to-back cljs / clj web app sold as a service - when finding an answer is painful enough you are less likely to forget the answer :D

ablazevix20:11:56

so you sort of have night vision lol

ablazevix20:11:10

for some reason the file comes back as a json though

noisesmith20:11:34

that might need to be fixed on the server side in the headers coming from ring

ablazevix20:11:54

hmm, I thought I had a fix from that before

ablazevix20:11:00

(response/content-type base-response-map "text/csv")

noisesmith20:11:16

aha - now I remember

noisesmith20:11:28

yeah, that should have addressed that

ablazevix20:11:15

yea it's weird because it does succesfully add the header, not sure what went wrong

noisesmith20:11:30

I wonder if the multi-hop of xhr (maybe with csrf in there too) might cause the browser to use a different header than the one in your dedicated handler?

noisesmith20:11:38

(just speculating here...)

ablazevix20:11:18

will leave it up to the sr engineers! thanks for the help tho

noisesmith20:11:05

there should be clues in the network history in the debug console if you have the right dev tools turned on, but if you have srs who can handle stuff easily that's always an option

ablazevix20:11:49

yep, spoke to the chief, and he said json is a server default, will do some changes that should fix that

sova21:11:10

How can I make sure I am writing an integer to the atom and not string?

sova21:11:15

reset! vs assoc-in?

sova21:11:58

nested vals

gerred21:11:52

@seancorfield btw thanks to Slack the ! is not recognized as part of the link. 😄

gerred21:11:07

which depending on the writer's mood might or might not seem intentional. 😉

sova21:11:10

lol yeah

sova21:11:50

turns out i'm a genius and was saving strings

sova21:11:58

B-) caps lock is cruise control for cool

seancorfield21:11:44

@gerred Thanks Slack 😐

gerred21:11:42

slack is there to help you against your own will.