Fork me on GitHub
#clojurescript
<
2017-07-30
>
fcrick02:07:29

hi beginner with qn - if i defn a function, how do i get the definition itself as a value? like, if i wanted to traverse the code of the function and print out each symbol or something like that

fcrick02:07:59

would i have had to quote the fn before i evaluated it? i think i just figured out how to do that

fcrick02:07:54

ooh thanks

sundarj02:07:54

no worries 🙂

sundarj02:07:48

looks like it does a println, so to get it as data you'd need to wrap it in a https://clojuredocs.org/clojure.core/with-out-str

sundarj02:07:36

ah wait, we're in #clojurescript, my bad - to get the js source of a function you can just do (.toString foo)

fcrick02:07:43

that gives me back the javascript

fcrick02:07:05

so what is the difference between and '`, exactly?

fcrick02:07:35

guess i still have some reading to do

noisesmith02:07:28

@fcrick you can google for "syntax-quote"

fcrick03:07:50

this is kind of blowing my mind

cljs.user=> '`[1]
(cljs.core/vec (cljs.core/sequence (cljs.core/concat (clojure.core/list 1))))

noisesmith03:07:10

that behavior makes ~@ possible

fcrick03:07:02

so how would i make something like

(let [x (list 2 3)] '`[1 ~@x])
not contain x in the result, but only 2 and 3

noisesmith03:07:42

by not using '

fcrick03:07:26

like, i still want the crazy (core/vec) stuff in there

noisesmith03:07:08

+user=> (let [x (list 2 3)] `[1 ~@x])
[1 2 3]
+user=> (let [x (list 2 3)] '`[1 ~@x])
(clojure.core/apply clojure.core/vector (clojure.core/seq (clojure.core/concat (clojure.core/list 1) x)))

fcrick03:07:19

i guess what i mean is how would i get something to return the same as this

cljs.user=> '`[1 2 3]
(cljs.core/vec (cljs.core/sequence (cljs.core/concat (clojure.core/list 1) (clojure.core/list 2) (clojure.core/list 3))))
but with the let part still in there

fcrick03:07:59

or maybe just an explanation as to what in particular makes that a silly qn heh

fcrick03:07:34

cljs.user=> (let [y (let [x (list 2 3)] `[1 ~@x])] `(list ~@y))
(cljs.core/list 1 2 3)
almost 😕

fcrick03:07:16

funnily enough, if i remove the last backtick in that, the browser-based repl i'm playing with crashes

fcrick05:07:13

any web based repl's out there that let you define and use macros?

sbrg11:07:25

What is a nice way of generating a range of characters in clojurescript? Apparently, \a doesn't work in cljs

yalu11:07:52

@sbrg I have not used it but this has char-range https://github.com/daveyarwood/djy

yalu11:07:59

nvm seems to be Clojure only

lsenta11:07:27

Hi #clojurescript I'm trying to speed-up my library load time by picking a few components at compile time, to make the generated js smaller

lsenta11:07:15

I have a macro that calls:

(defn load-def [def-name]
  (log/warn "Loading dynamically the Definition:" def-name ", you should recompile catalyst instead.")
  (let [p (get name->package def-name)
        bot-def (symbol (str p "/raw-rules"))]
    (do (require [p])
        @(resolve bot-def))))
At compile time I get a Arguments to require must be quoted

lsenta11:07:00

I other word, if I have (def p 'my-package), what's the right way to (require p) ?

moxaj11:07:58

@lsenta hm.. can you try (clojure.core/require ...)?

lsenta11:07:53

@moxaj thanks for taking a look, I get the same error. The full message:

Caused by: clojure.lang.ExceptionInfo: Arguments to require must be quoted. Offending spec: [p] at line 21 src/catalyst/bots/core.cljc {:file "src/catalyst/bots/core.cljc", :line 21, :column 9, :tag :cljs/analysis-error}
 

moxaj11:07:01

yeah, it seems you're seeing the clojurescript macro, not the clojure one

moxaj11:07:22

are you on 1.9.671?

moxaj11:07:41

@lsenta some fellow was having a similar issue and solved it by reverting to 1.9.660

lsenta11:07:28

I'm on 521

moxaj11:07:57

you can temporarily fix it by wrapping your load-def in a :clj reader conditional

moxaj11:07:17

(if you don't care about self-hosted)

moxaj11:07:41

or even better, just wrap the require form

thheller12:07:24

@lsenta what you are trying to do it not possible in code only via configuration

thheller12:07:36

CLJS does not support dynamic requires

moxaj12:07:20

@thheller but he's calling require at compile time, shouldn't that resolve to clojure.core/require, and thus work?

thheller12:07:44

require in a macro loads the CLJ namespace not a CLJS one

thheller12:07:53

so it does not affect the CLJS build at all

moxaj12:07:16

I think you misunderstood what I meant

thheller12:07:16

ah yeah I get it now … you want to emit (require '[some.symbol]) ie. you need the quote

thheller12:07:30

still not something you should be doing in code IMHO

moxaj12:07:42

I don't think that's the issue either 🙂

moxaj12:07:47

there's a require macro in both clojure and clojurescript

lsenta12:07:06

I think that's because (require x) not quoted will throw when the cljs compiler goes over it

lsenta12:07:18

moving everything in the :cljs conditional worked

lsenta12:07:54

My issue now is that the log'd or println'd messages during the macro are output to the compiled js code O.o

thheller12:07:09

cljs.util/debug-prn fixes that

lsenta12:07:40

I get an unreachable warning now:

Jul 30, 2017 2:13:36 PM com.google.javascript.jscomp.LoggerErrorManager println
WARNING: /target/cljsbuild-compiler-3/cljs/core.js:533: WARNING - unreachable code
}catch (e23214){var e_23219 = e23214;
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Jul 30, 2017 2:13:36 PM com.google.javascript.jscomp.LoggerErrorManager println
WARNING: /target/cljsbuild-compiler-3/cljs/core.js:586: WARNING - unreachable code
}catch (e23226){var e_23231 = e23226;
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Jul 30, 2017 2:13:36 PM com.google.javascript.jscomp.LoggerErrorManager printSummary
WARNING: 0 error(s), 2 warning(s)
but the compilation is finally going through, I'll keep this one for later

lsenta12:07:26

Thanks for the help @moxaj and @thheller Here's the version that compiles, if that can help someone:

(ns catalyst.bots.core
  (:require [catalyst.core :refer [make]]
...
  #?(:cljs (:require-macros [catalyst.bots.core :refer [preload-bots]])))

(def name->package
  {:helloworld    'catalyst.bots.bot-001-helloworld
   :helloyes      'catalyst.bots.bot-002-helloyes
   :whatsyourname 'catalyst.bots.bot-003-whatsyourname
...
})

#?(:clj
   (do
     (defn load-def [def-name]
       (cljs.util/debug-prn "Loading dynamically the Definition:" def-name ", you should recompile catalyst instead.")
       (let [p (get name->package def-name)
             bot-def (symbol (str p "/raw-rules"))]
         (do (clojure.core/require p)
             @(resolve bot-def))))

     (defmacro preload-bots []
       (let [def-name (keyword (System/getenv "REACT_APP_BOT"))
             data (load-def def-name)]
         (cljs.util/debug-prn "Compiling macro with def-name =" def-name)
         (when def-name
           `(def preloaded
              {~def-name (make ~data)}))))))

(preload-bots)

(defn ^:export rules-for [bot-name]
  (let [k-bot-name (keyword bot-name)]
    (assert (contains? name->package k-bot-name)
            (str "Missing bot:" bot-name))
    (get preloaded bot-name)))

thheller12:07:17

be aware that caching will not be aware of (System/getenv "REACT_APP_BOT") so incremental builds won’t work (if you change it)

rnagpal17:07:07

I need to read about how exactly clojurescript code compiles to javascript

rnagpal17:07:26

is there some documentation around it ?

mfikes17:07:02

@rnagpal Maria Gheller gave general talk on how the compiler operates. http://m.youtube.com/watch?v=Elg17s_nwDg

rnagpal17:07:05

Thanks @mfikes This video is really good. Didn’t find much apart from this video

mfikes17:07:55

@rnagpal The compiler source is fairly easy to understand

dnolen17:07:14

@rnagpal Maria Geller’s talk pretty much covers it - for details I agree with @mfikes, you’ll probably just want to read through the analyzer and the compiler sources

dnolen17:07:46

also #cljs-dev is a good place to lurk if you’re generally interested in this stuff

benbot18:07:32

Is there a way to call functions on a javascript object that i declared within clojurescript? I'm trying to call a function on a reagent component, but can't figure out how

mobileink19:07:54

@benbot what have you tried?

benbot20:07:39

@mobileink I tried calling the function on the value returned by the reagent function. But it said the function being called didn't exist

mobileink20:07:15

code snippet?

benbot20:07:31

@mobileink I'm not near my computer right now :( but do you have any idea how to call it correctly?

mobileink20:07:16

nope. 😉 not really a ninja but have been messing with interop. 1st: check your syntax (.foo bar) with dot.

mobileink20:07:50

2nd: try #reagent

benbot20:07:25

Oh my syntax might be off

chris_23:07:53

Hello fellow Clojurians, How can I get just the “raw HTML string” from an ajax GET request? (I’m making a GET request to >) With Clojure, I can simply slurp the url (which I then parse and scrape with Hickory, etc.). Clojurescript doesn’t have slurp. So I’m instead making an AJAX GET request to welcome-url. (Or is there a simpler way?). When I console-log the response of the GET request, I see the HTML string I want to parse, but the response itself is a Javascript object Object. How can I simply get the “raw HTML string”? (Apologies if this isn’t the right place for these kinds of questions?)

chris_23:07:04

Some relevant code:

(defn handler [response]
  response)

(defn error-handler [{:keys [status status-text]}]
  (.log js/console (str "something bad happened: " status " " status-text)))

(defn get-request-word [a-url]
  (GET
    a-url
    {:handler handler :error-handler error-handler}))

akiroz23:07:19

@chris_ You're using the cljs-ajax library here right? the response map has a :body key which should contain the data you want.