clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
phronmophobic 2021-03-18T00:04:39.027900Z

you probably want something like:

(with-open [rdr (<http://clojure.java.io/reader|clojure.java.io/reader> "page")
            pb-rdr (java.io.PushbackReader. rdr)]
  (clojure.edn/read pb-rdr))

phronmophobic 2021-03-18T00:05:10.028100Z

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

macrobartfast 2021-03-18T00:05:40.028300Z

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

macrobartfast 2021-03-18T00:05:51.028500Z

It’s not technically an edn file.

phronmophobic 2021-03-18T00:07:54.028700Z

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

macrobartfast 2021-03-18T00:07:54.028900Z

throws

[ is not a valid element name.

phronmophobic 2021-03-18T00:08:23.029100Z

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

macrobartfast 2021-03-18T00:08:25.029300Z

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

phronmophobic 2021-03-18T00:08:33.029500Z

does the file contain edn?

phronmophobic 2021-03-18T00:10:30.029800Z

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

macrobartfast 2021-03-18T00:10:33.030Z

it doesn’t.

macrobartfast 2021-03-18T00:10:46.030200Z

just the plain vector.

macrobartfast 2021-03-18T00:11:55.030400Z

just

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

macrobartfast 2021-03-18T00:12:30.030600Z

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

[ is not a valid element name.

phronmophobic 2021-03-18T00:12:37.030800Z

the above code with the edn reader should work

macrobartfast 2021-03-18T00:12:45.031Z

ok, I can give that a go.

phronmophobic 2021-03-18T00:13:44.031200Z

you'll probably need to require:

(require '<http://clojure.java.io|clojure.java.io>
          'clojure.edn)

macrobartfast 2021-03-18T00:14:42.031500Z

seems to be available, but that throws

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

macrobartfast 2021-03-18T00:15:24.031700Z

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

phronmophobic 2021-03-18T00:15:42.031900Z

are you copied the code correctly?

phronmophobic 2021-03-18T00:16:03.032100Z

for eg.:

(with-open [rdr (<http://clojure.java.io/reader|clojure.java.io/reader> "deps.edn")
            pb-rdr (java.io.PushbackReader. rdr)]
  (clojure.edn/read pb-rdr))
This works for me

phronmophobic 2021-03-18T00:17:15.032300Z

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

macrobartfast 2021-03-18T00:18:05.032500Z

I got it!

(read-string (slurp "page"))

2
macrobartfast 2021-03-18T00:18:38.032800Z

and totally grateful for the help!

👍 1
frerom 2021-03-18T12:20:54.038600Z

I'm looking into licenses and ClojureScript is under EPL along with a lot of ClojureScript libraries. Since JavaScript in a browser is technically a distribution (from what I understand) then anyone developing a website with ClojureScript would have to disclose the source code and also include the EPL license. Is my understanding of this correct?

phoenixjj 2021-03-18T12:39:07.039200Z

"Linking to code (for example to a library) licensed under EPL automatically does not mean that your program is a derivative work."

phoenixjj 2021-03-18T12:39:39.039800Z

As long as you are not modifying clojurescript or lib source code you are fine.

phoenixjj 2021-03-18T12:39:54.040200Z

this is what I understand.

frerom 2021-03-18T12:50:45.041100Z

Thanks @phoenixai2082, that would make sense. If anyone else has a different view, please share!

fsd 2021-03-18T15:08:15.042500Z

Hi There, I am working on a react-app, I have this function for for map icons, I am having an issue when I click on Icons in  `yarn start`  it works (it gets the id), but when I use the `yarn build`  build file and click on Icon, it gets id as nil. I am not sure whats causing this to behave this way. Uncaught (in promise) Error: Vector's key for assoc must be a number.

(defn on-click-map-icons [event props page]
     (def feature (first event.features))
        (println feature.properties.itemid)
    (when (= page "results")
    (if   (= feature.source "selected-item") (router/redirect! props :router {:page "item" :item (:id {:id itemid })}) ))
    (when (= page "item")
        (if (= feature.source "subitem")
          (router/redirect! props :router {:page "item" :item (:id {:id itemid } ) :subitem (:sub-id {:sub-id subitemid})  }))))

p-himik 2021-03-18T15:13:57.042700Z

Don't use . that way. Stuff like event.features should be (.-features ^js event). The ^js part can be there only once where you first define the name - in this case, right in the signature, like [^js event props page].

fsd 2021-03-18T15:33:21.043100Z

Thanks @p-himik for pointing that out, I’ve tried using

(defn on-click-map-icons [event props page]
     (def feature (first (.-features ^js event)))
    (when (= page "results")
    (if   (= feature.source "selected-item") (router/redirect! props :router {:page "item" :item (:id {:id feature.properties.itemid })}) ))
    (when (= page "item")
        (if (= feature.source "subitem")
          (router/redirect! props :router {:page "item" :item (:id {:id itemid } ) :subitem (:sub-id {:sub-id feature.properties.subitemid})  }))))
Still got Error: Vector's key for assoc must be a number.

p-himik 2021-03-18T15:37:09.043500Z

I didn't mean just that interop with event - I meant all interop field access in your code.

p-himik 2021-03-18T15:38:04.043700Z

Also, you probably want to use let instead of def.

fsd 2021-03-18T15:45:08.043900Z

Ah I see

fsd 2021-03-18T16:01:55.044100Z

@p-himik I am able to get to properties like

(.-properties ^js feature) 
#js {:title sample_note,  :noteid 3}
how do I go inside the object and get title? For example feature.properties.title

p-himik 2021-03-18T16:07:24.044400Z

Choose any one or a combination or them:

(.. feature -properties -title)
(.-title (.-properties feature))
(-&gt; feature .-properties .-title)
(. (. feature -properties) -title)

p-himik 2021-03-18T16:08:16.044700Z

CLJS/JS interop and CLJ/Java interop are almost identical. Completely identical if you don't go outside of the . and -.

fsd 2021-03-18T16:33:41.044900Z

Thank You

2021-03-18T17:51:24.047100Z

When using webpack in javascript, I can do something like:

function dynamic-load() {
  require("path/to/module");
}
Is there any way to do this sort of thing in clojurescript (when using the :bundle target)?

p-himik 2021-03-18T19:41:27.047200Z

No, all requires have to be a part of an ns form. I'm not familiar with dynamic load in webpack. Is it loading the module during the run time, when e.g. some relevant component is requested? If so, then CLJS have module splitting for that.

martinklepsch 2021-03-18T19:59:26.049400Z

I’m using ^:export to export a sort-of feature flag to be changed from the console but it looks like the boolean value is inlined wherever it is being used, meaning setting it does not actually propagate to the various sites where it’s used. Is there a way to avoid this type of inlining for specific vars? (In the meantime I fixed this by using an atom.)

dnolen 2021-03-18T20:13:07.050100Z

@martinklepsch I don't think you can export value? I recall running into this - only functions

👍 1
lilactown 2021-03-18T23:02:32.051700Z

writing a CLJC lib that returns either Clojure promise or a JS Promise and really missing async/`await` 😞

lilactown 2021-03-18T23:03:03.052200Z

gotta split my test suite between CLJ and CLJS, even though the semantics are basically the same

lilactown 2021-03-18T23:12:49.053Z

the dream would be I could do:

#?(:clj @(f/send n msg)
   :cljs (await (f/send n msg)))
Alas,

2021-03-19T08:52:50.059200Z

You can't use &lt;p! ?

nilern 2021-03-19T09:22:22.059400Z

The test framework has no more special support for core.async than for Promises and I suspect &lt;p! is cljs-only as well

2021-03-19T15:57:41.115Z

Well I was thinking cause he wants to block on the promise return is my guess. So if he makes the cljs code in a go block with <p! he'd get the blocking behavior. But I guess not really since the go block itself will be async now. The same way you said await would just move the problem up as well.

2021-03-19T15:58:44.115200Z

I think I'd just make the clj code async as well. Maybe use CompletableFuture instead of a Clj promise, since those have a much similar interface to JS promises

2021-03-19T16:00:16.115400Z

Using Promesa maybe could also help. It is already Clj and Cljs compatible.

lilactown 2021-03-19T17:21:00.115900Z

I did end up using core.async and &lt;p!

lilactown 2021-03-19T17:21:32.116100Z

it still required me to split my tests between CLJ and CLJS as I needed to wrap things in async and go

lilactown 2021-03-19T17:21:52.116300Z

In general I'm trying not to pull in too many external libs

2021-03-19T18:11:46.117300Z

Fair enough. I think nilern is right though, I'm not sure await would have helped, because you can only use await inside of async, similar to how you can only use <p! inside go. So you might have had to shenanigan things either way. It's the whole colored function problem.

lilactown 2021-03-19T18:21:14.117900Z

yeah like I said, I'm stating my dream; not a call to action 😛

2021-03-19T19:07:01.126600Z

Haha ya. Its going to get a bit weirder too, because Java will get green threads, and CLJ will probably use that for async. But CLJS will need to stick to async/await or go/<p!

2021-03-19T20:00:13.127300Z

Oh, nice solution you found in the end!