Fork me on GitHub
#clojurescript
<
2022-01-04
>
Muhammad Hamza Chippa20:01:59

{"user_account" [{"user_account_id" 1, "user_type" 0}]} I have this type of data in the clojurescript file, I wanted to convert string into keyword how can I do that clojure.data.json is not working in the clojurescript file ?

p-himik20:01:23

Would be more suitable for #beginners.

seancorfield20:01:51

(it got answered in #beginners just in case anyone else checks this thread)

ns21:01:50

I'm trying to implement cache in my application for a bunch of key value pairs which would keep only the latest X number of pairs (perhaps a few thousand or so). The older ones would be discarded as the new ones are being added. Currently I'm using a map inside an atom but since maps do not keep the insertion order I don't know how to discard the older entries. I'm reading about array maps but those are only for smaller arrays because of performance reasons. One solution I can think of is to keep a vector of keys parallel to the map which would keep the insertion order but it doesn't seem very elegant solution to me. Any ideas how to go around this problem?

Alex Miller (Clojure team)21:01:44

well you could just use a core.cache with LRU

ns21:01:29

Does this work for Clojurescript as well? (Sorry I'm pretty new to this)

Alex Miller (Clojure team)21:01:55

no, sorry, didn't realize we were in cljs channel :)

ns21:01:48

@alexmiller It's ok, thanks anyways 🙂

borkdude22:01:18

@nedim searching on the web for "lru cache javascript" comes up with a couple of helpful things for implementing your own or as a JS library

kennytilton22:01:04

So I am doing that trick with x.cljs requiring macros from x.clj, specifically :

(ns mxreact.mxreact
  (:require-macros [mxreact.mxreact])
  ...etc)
... and I am getting apparently warnings such as WARNING: map already refers to: #'clojure.core/map in namespace: mxreact.mxreact, being replaced by: #'mxreact.mxreact/map The problem is that I am indeed generating macro map and a couple others because I am generating wrappers for all known HTML tags, and map is an HTML tag, so in it goes. Looking at https://cljs.github.io/api/cljs.core/ns, I see a promising:
(:require-macros
    lib.ns
    [lib.ns :refer []
            :rename {}
            :as alias]
    :reload
    :reload-all)
... but I want all tags other than map to be accessible without using the alias. Hmm, I recall :refer :all being a non-starter for CLJS. Has that changed? I see other variants of require that look promising, but was told to use :require-macros and I always do what I am told. The ugly (easy) way out would be NOT to generate the conflicting macros and code those up manually with names like html-map, but it would be neat to leverage some cljs ns flexibility to do that. I thought I would drop in here for guidance before turning my thousand monkeys loose at the their keyboards trying various incantations. Suggestions welcome! 🙏

borkdude22:01:41

You can get rid fo the warning by doing :refer-clojure :exclude [map]

kennytilton23:01:40

Thx! But I might need these other tags, so I want to rename them. Well, maybe I can :exclude [map] and then add an :alias mxweb and let the rare usage do (mxweb/map ...)?

thheller06:01:31

the warnings you get are from the CLJ namespace and :require-macros is not the right place to do what you want to do. :require-macros is not producing this warning. see how fulcro has done it https://github.com/fulcrologic/fulcro/blob/develop/src/main/com/fulcrologic/fulcro/dom.clj#L20

thheller06:01:49

consumers can do (:require [mxreact.mxreact :as mx]) (mx/map ...) just fine without any aliasing or whatever

thheller06:01:46

I also have an older impl setting up the dom elements as macro/fn combo, also note the :exclude https://github.com/thheller/shadow/blob/master/src/main/shadow/markup/react.clj

leif22:01:49

I'm trying to match a multi-line string wit a regex, but when I compile I get: ERROR: JSC_LANGUAGE_FEATURE. This language feature is only supported for ECMASCRIPT_2018 mode or better: RegExp flag 's'.

leif22:01:12

Is there anything I can do for that, or do I need to just refactor my code to not use re-find?

Cora (she/her)23:01:37

so I don't have a solution for you but I was curious and I searched for ECMASCRIPT_2018 here in slack and a lot of stuff popped up. it seems like you can change the js version target and it'll be fixed?

leif23:01:22

Ya, that makes sense.

leif23:01:49

I'm just weary of doing that since I don't know what other effects it will have.

leif23:01:17

But anyway, I was able to get rid of the regex and just wrote out the code I needed by hand. Oh well. 😞

leif23:01:20

Thanks anyway btw.

misha12:01:36

s flag makes . behave like (?:.|\n) (anything or newline in non-capturing group) replacing dots with with that might be feasible for you. it uglyfies reg ex a bit, but it is an option:

misha13:01:01

(throw an \r there too, I guess)

ns22:01:45

@borkdude thanks, js solution would be a bit easier since Maps in js keep the insertion order so I guess I could do some interop, but also searching for lru cache clojurescript I came upon CLJS port of clojure/core.cache https://github.com/burbma/cljs-cache though it hasn't been updated in a while (perhaps it doesn't need to). I'll look into it, thanks!

emccue23:01:32

well, one way is to just make a ring buffer

emccue23:01:49

{:i    0
 :keys (vec (repeat 2000 nil))
 :data {}}

emccue23:01:59

to insert you just check your current place in the :keys vector

emccue23:01:33

(defn insert [{:keys [i keys data]} k v]
  (if-let [existing (nth keys i)]
      {:i    (mod (inc i) 2000)
       :keys (assoc keys i k)
       :data (-> data
                 (dissoc existing)
                 (assoc k v))}
      {:i    (mod (inc i) 2000)
       :keys (assoc keys i k)
       :data (assoc data k v)})
    

emccue23:01:50

something like that. What i wrote wont work but thats the general direction i would go

ns23:01:32

@U3JH98J4R Thanks, I'll give it a try!