Fork me on GitHub
#beginners
<
2020-04-19
>
Old account10:04:09

will core.async channels get GCd if unclosed?

Lukas16:04:13

Hi, im trying to bring some data into the right shape but for some reason a lot of "nil" value remain and i'm not able figure out why i can't remove them.

(defn git-log [dir]
  (->
   (sh "git"
       "-C" dir
       "log"
       "--pretty=format:'[%h] %an %ad %s'"
       "--date=short"
       "--numstat")
   :out
   (str/split-lines)))

(defn apply-schema [data]
  (let [schema [:additions :deletions :name]]
   (->>
    data
    (filter (fn [x] (not (or (str/starts-with? x "'[")
                             (str/starts-with? x "-")))))
    (map (fn [x] (str/split x #"\t")))
    ;(map (fn [x] (zipmap schema x)))
    (filter #(= (count %) 3))
    (filter #(some? %)))))
these are the functions im using and this is a part of the result:
nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil nil [17 0 metro.config.js]
[9936 0 package-lock.json]
i also tried to filter for "nil" as string but they still remain 😬 Any pointer what i'm doing wrong here are greatly appreciated ☺️

Lukas17:04:49

nvm i guess this is just the println representation 🙈

Aron17:04:49

why is my response status 0 and body "" for a cljs-http? I mean, I see it should be 403 from the server side (django)

dpsutton18:04:45

Do you have your browser console open?

Aron19:04:19

was this an answer to me? 🙂

dpsutton19:04:33

if its a browser based repl there often is more information in the console. and i suspect its cors issues which should be reported there in the browser

Aron19:04:49

I don't think there are any repls

Aron19:04:57

I was writing out the response like this

!   2 (defn init-req                                                                       
!   3   [asset-type]                                                                            
!   4   (when-not (string/blank? asset-type)                                                    
!   5     (go (let [response (<! (http/post ""         
!   6                                       {:body {"asset_type" asset-type}}))]                
!   7           (prn 'status (:status response))                                                
!   8           (prn 'resp (:body response))))))    

dpsutton20:04:20

This code is compiled to JavaScript and run in the browser unless your target is node. If it’s a browser can you open up the dev console?

Aron20:04:14

but it just shows the same thing, 403, some cors error, which i think i know the cause of

dpsutton20:04:04

Excellent. Then you have solved your issue

dpsutton20:04:37

have you solved this issue? you aren't seeing anything in cljs because of the CORS issue

dpsutton20:04:41

if you solve that it will work

Aron20:04:42

what are you talking about??

dpsutton20:04:56

The CORS issue is your problem

Aron20:04:05

what is excellent, what has been solved? I haven't got an answer and nothing changed, how can you say that's "excellent"?

Aron20:04:12

what excels in this?

dpsutton20:04:21

that you have identified your issue

Aron20:04:28

no, my problem is that I don't see the same value in clojurescript as the server returns

dpsutton20:04:38

and you said about the CORS issue > which i think i know the cause of

Aron20:04:41

I asked specifically about this, not something else

dpsutton20:04:44

right because of the CORS issue

dpsutton20:04:51

it is the CORS issue

dpsutton20:04:14

the browser doesn't give you responses to javascript fetches when there's a CORS issue

dpsutton20:04:51

cljs-http cannot work if there's a CORS issue. The browser's protections prevent it from seeing the response

Aron20:04:51

i am extremely upset and frustrated right now, how can I express this without getting banned? I asked one question and now I am being told that I should just ignore my question because here is another question with an answer, isn't it good that this different question has an answer?

dpsutton20:04:08

i am literally answering your question

dpsutton20:04:13

> Note: For security reasons, specifics about what went wrong with a CORS request are not available to JavaScript code. All the code knows is that an error occurred. The only way to determine what specifically went wrong is to look at the browser's console for details.

dpsutton20:04:39

what should i read from there?

dpsutton20:04:57

can you share the url you are requesting?

dpsutton20:04:47

i believe you are seeing this:

dpsutton20:04:49

(go (let [response (<! (http/get "localhost:3000"))]
      (prn (:status response))
      (prn response)))
#object[cljs.core.async.impl.channels.ManyToManyChannel]
client=> 0
{:status 0, :success true, :body "", :headers {}, :trace-redirects ["localhost:3000" "localhost:3000"], :error-code :no-error, :error-text ""}

Aron20:04:12

thanks, you are correct. I mean, obviously you were 100% correct before, but I needed to be told something else. In fact, indirectly, from your answer, I googled just cors and status 0 and then I found that actually there are several reasons that can cause this and cors is just one.

Aron20:04:41

knowing it was not a clojurescript issue helped a lot!

Old account19:04:46

why we need dynamic vars if we can just use def again? Or I miss something?

salam19:04:55

def creates a new var (shadowing any var with an existing symbol associated to it), alter-var-root and friends mutate an existing var.

phronmophobic19:04:41

the other use case is to simulate dynamic scoping using something like binding.

teodorlu19:04:52

How can I create a function which wraps its dependency, and doesn't require the dependency untill the function is called?

(defn dynamic-dep-join [a b]
  ;; Imagine this (require) is ridiculously slow
  (require 'clojure.set)

  ;; Now, how do I call the function without requiring a "static reference"?
  ;; The following wouldn't compile unless "by luck" it has been required elsewhere
  (clojure.set/join a b))

dpsutton19:04:05

i think something like (let [union (requiring-resolve 'clojure.set/union)] (union #{1 2} #{3 4}))

💯 4
dpsutton19:04:28

an example from clojure/main.clj

(with-out-str
  (binding [*print-namespace-maps* false]
    ((requiring-resolve 'clojure.pprint/pprint) report)))

💯 4
teodorlu19:04:27

requiring-resolve was just what I was looking for. Thanks a lot! Also, congrats on guessing that I meant to use union for the example 😆

(defn dynamic-dep-union [a b]
  (require 'clojure.set)
  (let [f (requiring-resolve 'clojure.set/union)]
    (f a b)))

(comment
  (dynamic-dep-union #{:a :b} #{:b :c})
  ;; => #{:c :b :a}
  )

Aron19:04:41

what's the default option for reading browser cookies?

Aron19:04:49

just js interop?

teodorlu19:04:38

@ashnur are you on Clojure or Clojurescript?

Aron19:04:29

it would be very interesting to see how to read out browser cookies using clojure 🙂

Aron19:04:35

but no, i am in cljs

Aron19:04:38

shadow-cljs

teodorlu19:04:49

I'm not an expert at this, but I think there are HTTP conventions for how to read and set cookies using header values. See the Ring docs: https://github.com/ring-clojure/ring/wiki/Cookies

teodorlu19:04:28

For CLJS, sorry, don't know. Perhaps ask in #clojurescript?

Aron19:04:55

this is a beginner question so i asked here

Aron19:04:15

i don't think ring helps me read browser cookies

David Pham19:04:06

Are there valid reason to use eval when creating macro? I created a macro that defines a function, and I would like to loop over a sequence of argument, but I can’t manage to evaluate my macro without eval.

phronmophobic20:04:54

there may be a reason to use eval in a macro, but it’s really unlikely. do you have a code example? it’s hard to give pointers without more info

teodorlu20:04:28

I also suspect that you'll be able to make this work without eval. Normally, a macro just returns source code, which will be evaluated normally when the macro completes. I agree with @U7RJTCH6J that a specific example would be helpful :)

David Pham21:04:49

I will try to write one tomorrow! I have to sleep. But basically, a macro (defmacro m [function-name path]) that defines a a function with name function name. Now I have a collection of [function-name path] and I want to loop over that collection and evaluate my macro on each elements.

David Pham21:04:08

Thanks for your help.

David Pham21:04:06

I tried mapv, loop recur, but my issue is I can not finish with a (do (m f_1 p_1) ... (m f_n p_n))

teodorlu21:04:19

:thumbsup: 🌃

David Pham06:04:31

Here is the example

David Pham06:04:37

(defmacro reg-fn+path
    [fname p]
    `(defn ~(symbol fname)
       ([] ~p)
       ([m#] (get-in m# ~p))
       ([m# v-or-fn#] (assoc-in m# ~p v-or-fn#))
       ([m# f# & args#] (apply update-in m# ~p f# args#))))

  (defmacro reg-fns+paths [fns+paths]
    `(for [[f# p#] ~fns+paths]
       (eval `(reg-fn+path ~f# ~p#))))

phronmophobic07:04:40

@UEQGQ6XH7, maybe something like:

(defmacro reg-fn+path
    [fname p]
    `(defn ~(symbol fname)
       ([] ~p)
       ([m#] (get-in m# ~p))
       ([m# v-or-fn#] (assoc-in m# ~p v-or-fn#))
       ([m# f# & args#] (apply update-in m# ~p f# args#))))
(defmacro reg-fns+paths [fns+paths]
  `(do
     ~@(for [[f p] fns+paths]
         `(reg-fn+path ~f ~p))))
(macroexpand-1 '(reg-fns+paths [[foo :foo] [bar :bar]]))
;; (do
;;   (my.ns/reg-fn+path foo :foo)
;;   (my.ns/reg-fn+path bar :bar))

David Pham07:04:13

Can you have the arguments be stored in a variable? The [[foo :foo]]?

phronmophobic07:04:56

but you could if you use a regular function

phronmophobic08:04:03

making a function similar reg-fn+path is not usually going to buy you very much. using (foo my-map) isn’t much short than (:foo my-map) or (assoc my-map :foo v) vs (foo my-map v)

phronmophobic08:04:40

but you could create a similar function that’s not a macro like:

(defn reg-fns+paths [fns+paths]
  (doseq [[f p] fns+paths]
    (intern *ns* f  (fn ([] p)
                      ([m] (get m p))
                      ([m v-or-fn] (assoc m p v-or-fn))
                      ([m f & args] (apply update-in m p f args))) )))

(let [fns+paths '[[foo :foo] [bar :bar]]]
   (reg-fns+paths fns+paths)) 

teodorlu09:04:25

@U7RJTCH6J Nice to see your solution totally avoiding macros 🙂 I've been surprised a few times about the function / macro distinction. Using intern is just a function call. So there's no need for macros, even though you're doing a kind of code generation.

David Pham12:04:14

My issue is it does not work for CLJS 😞

teodorlu13:04:49

Ah, right. Vars may be Clojure-only. In that case, I think you'll have to use a macro to generate some (do (defn first-fn ,,,) (defn second-fn ,,,),,,)

phronmophobic18:04:22

fundamentally, values generated in clojurescript aren’t available at compile time. there are a couple workarounds, but it depends on the use case.

justin22:04:48

Hello! I'm trying to use react-moveable (https://github.com/daybrush/moveable/tree/master/packages/react-moveable) in a ClojureScript project. I installed the dep by modifying project.clj and adding :npm-deps and :install-deps true. I'm using figwheel for development. In styler.esm.js there is a reference to .`(module$Users$somebody$proj$threat_modeler$node_modules$react$index.Component);` The Component attribute is undefined, and it's raising an error when I'm trying to require react-moveable . The property index.default.Component exists though. I read somewhere ".default" is a es5 thing...anyone ran into something similar before?

David Pham03:04:39

Did you try to build with shadow-cljs?

justin04:04:40

No, but I will give it a shot!

justin06:04:16

Moving to shadow-cljs seems to of fixed the error 🙂. Thanks!