clojurescript

ClojureScript, a dialect of Clojure that compiles to JavaScript http://clojurescript.org | Currently at 1.10.879
Jacob Rosenzweig 2021-05-28T01:14:58.379500Z

I just realized that I could probably get away with inserting clojurescript into one-off projects I do at work.

Jacob Rosenzweig 2021-05-28T01:15:50.380600Z

E.g. I made a "return to campus" bot for our employees and I'm basically under no review for that. I guess I'd have to go to make a ticket to get the cljs compiler approved, but that wouldn't be hard.

😎 2
Jacob Rosenzweig 2021-05-28T01:20:52.380900Z

Kind of OT but @dnolenΒ I am watching an old talk you gave and I still am surprised that some projects still try to make "readable javascript". "Rescript" (a rebranding of "Reason" from Facebook) attempts to reproduce readable javascript as well.

Stas Makarov 2021-05-28T13:22:35.386700Z

I've just got a strange bug while playing with cljs on aws lambda (via node). I have this code:

(defn handler [event ctx callback]
   (println "event:  " event)
   (println "http method " (.-httpMethod event) (. event -httpMethod) (goog.object/get event "httpMethod") (aget event "httpMethod"))
.... 
In cloudwatch logs I get: INFO event: #js {:resource /patients, :path /patients, :httpMethod POST, ... INFO http method nil nil POST POST Why (.-httpMethod event) and (. event -httpMethod) might not work as expected?

Stas Makarov 2021-05-28T13:25:15.388100Z

When I'm calling handler locally with event bound to #js{:httpMethod "POST" ... } I get expected result.

βœ… 1
p-himik 2021-05-28T13:33:03.388200Z

I'm guessing advanced compilation. httpMethod and other field names were mangled. Add ^js in front of event in the function signature. Words for looking up why: "clojurescript externs inference".

Stas Makarov 2021-05-28T13:34:23.388400Z

Thank you! I'll look into it.

Stas Makarov 2021-05-28T13:57:38.388700Z

hehe "Externs or rather the lack thereof are often to blame here and possibly the most frequent issue coming up in the #clojurescript Slack (and elsewhere)." ( https://code.thheller.com/blog/shadow-cljs/2017/10/15/externs-the-bane-of-every-release-build.html)

p-himik 2021-05-28T14:02:29.389200Z

Yep. :)

kah0ona 2021-05-28T14:19:27.390700Z

Hello Folks, how can I convert this piece of javascript to clojurescript (using interop?) view[i] = s.charCodeAt(i) & 0xFF

kah0ona 2021-05-28T14:19:38.391Z

particularly the last bit ' & 0xFF '

kah0ona 2021-05-28T14:19:53.391300Z

the rest i can do with (aset)

kah0ona 2021-05-28T14:23:39.392300Z

ooooooooooooh there’s bit-and

borkdude 2021-05-28T14:31:57.393200Z

In which cases was aset not recommended again and should you use gobject/set instead? And when is it actually recommended to use aset ?

emccue 2021-05-28T14:32:39.393900Z

when its not a property like 0, 1, 2, 3, 4, 5 - as in an array

emccue 2021-05-28T14:32:59.394300Z

then gobject/set

borkdude 2021-05-28T14:33:02.394400Z

so for numeric indexes, use aset?

emccue 2021-05-28T14:33:14.394800Z

i think so

borkdude 2021-05-28T14:33:43.395300Z

and when should you use gobject/set instead of set! (aside from set! not being as dynamic)? when dealing with Closure advanced, I guess?

jerger_at_dda 2021-05-28T16:23:59.396300Z

Hi, does anybody know a good base64 encoder/decoder working on cljs & clj ?

flowthing 2021-05-28T16:34:56.396400Z

If you're targeting the browser, I guess this is one option:

(ns my.base64
  #?(:clj (:import (java.util Base64))))

(defn base64-encode
  [string]
  #?(:clj (.encodeToString (Base64/getEncoder) (.getBytes string))
     :cljs (.btoa js/window string)))

(defn base64-decode
  [string]
  #?(:clj (String. (.decode (Base64/getDecoder) string) "UTF-8")
     :cljs (.atob js/window string)))

(comment
  (-> "Hello, world!" base64-encode base64-decode)
  )

flowthing 2021-05-28T16:38:04.396600Z

Can obviously reuse the encoder and decoder on the JVM β€” that's just an illustrative example.

jerger_at_dda 2021-05-28T17:02:42.396800Z

we're writing apps targeting to cljs & clj So I'm searching for a pure clojure implementation in order to minimize platform specific code parts ...

flowthing 2021-05-28T17:04:38.397300Z

Oh, I see. πŸ‘:skin-tone-2:

thheller 2021-05-28T17:31:44.397500Z

all the aset aget and related functions are for working with arrays, just like in clojure. in JS it just happens to also work for property access since it uses the same thing["foo"] and thing[0] notation. so aset for arrays, which also implies numeric indexes

thheller 2021-05-28T17:32:17.397700Z

goog.object when working with data, set! when working with "code"

jerger_at_dda 2021-05-28T17:39:23.397900Z

FYI found https://github.com/cloojure/tupelo

flowthing 2021-05-28T17:41:51.398200Z

The Base64 bits seem to be Clojure only and use Java interop?

jerger_at_dda 2021-05-28T18:24:12.398400Z

Jupp ... unfortunately ...

jerger_at_dda 2021-05-28T18:25:19.398900Z

now using different impl. for clj & cljs

flowthing 2021-05-28T18:31:09.399100Z

As I mentioned earlier, I believe you can reuse both the encoder and the decoder, instead of making a new one for each call. Also, you might want (.getBytes string "UTF-8") (unless you know you specifically want your platform's default encoding).

jerger_at_dda 2021-05-28T18:34:46.399300Z

πŸ‘

jerger_at_dda 2021-05-28T18:36:04.399500Z

thx πŸ™‚

2021-05-28T19:55:57.403100Z

I have a map that is used many places in the codebase. Sometimes keys are looked up on it that don't exist. I'd like to know what keys are being looked up and print them, store them somewhere, or whatever. My first thought is that I should be able to make a new type, inherit from PersistentHashMap and only implement ILookup. Is there a way to do that or do I really need to reimplement the entire map type?

alexmiller 2021-05-28T19:57:37.403300Z

proxy?

2021-05-28T20:22:25.403600Z

Does that exist in cljs?

cljs.user> proxy
WARNING: Use of undeclared Var cljs.user/proxy at line 1 <cljs repl>

alexmiller 2021-05-28T20:27:23.403800Z

oh, sorry didn't realize what channel I was in

2021-05-28T20:28:48.404Z

No worries! πŸ™‚

emccue 2021-05-28T20:30:33.404400Z

@bmaddy what about a defrecord?

2021-05-28T20:33:18.404500Z

Unfortunately, I don't know all the fields, so I don't think I can do that. More context: My map is being built from a response from Datomic and the pull expression has a wildcard in it that I'm trying to get rid of. πŸ˜• Good thought though.

emccue 2021-05-28T20:35:43.404700Z

defrecord doesn't require all the fields - its just a named type

emccue 2021-05-28T20:37:01.404900Z

i'm not sure how to get around the circularness of it - how to access the underlying "get" functionality if you are overriding it - but at the very least you can store any fields you want in a defrecord like (defrecord ABC [])

emccue 2021-05-28T20:37:55.405100Z

would be neat if ILookup could be extended via metadata - that would solve the issue a lot cleaner

emccue 2021-05-28T20:41:42.405300Z

https://blog.wsscode.com/guide-to-custom-map-types/

emccue 2021-05-28T20:41:51.405600Z

here is a reference that i remember reading forever ago

2021-05-28T20:42:59.406300Z

Yeah, we did find those. It looks like they say you need to implement the entire thing with deftype.

2021-05-28T20:43:09.406500Z

I haven't read the whole article though, just skimmed.

2021-05-28T20:44:55.406700Z

I hadn't realized deftype maps were open like that. Good to know!

emccue 2021-05-28T20:48:09.407600Z

here are all the intrinsic impls for a hashmap - you can maybe make a custom overridable map type helper given this

2021-05-28T20:48:44.407800Z

Yeah, I think that's what I'll have to do. Thanks for taking a look!

2021-05-28T22:00:51.408300Z

I know Figwheel and Reagent are pretty cool together for ClojureScript front-end. Is there anything really cool for direct output manipulation? https://youtu.be/jC2_O5Jh_Rg

2021-05-31T00:40:00.439400Z

Anyone from PenPot here?

borkdude 2021-05-28T22:02:51.408400Z

https://blog.wsscode.com/guide-to-custom-map-types/

Donnie West 2021-05-28T23:21:04.410100Z

Does anyone know a good way to interop w/ JS tooling that expects a default esm export in the index.js file using ClojureScript's build tooling? Trying something akin to https://clojurescript.org/guides/webpack but having trouble adapting it πŸ˜•

Donnie West 2021-05-28T23:24:17.410200Z

For that matter, shadow-cljs wouldn't be a terrible alternative if I could it to work too πŸ˜‚